@gitlab/duo-ui 8.11.0 → 8.12.1

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.
@@ -0,0 +1,876 @@
1
+ <script>
2
+ import throttle from 'lodash/throttle';
3
+ import VueResizable from 'vue-resizable';
4
+
5
+ import {
6
+ GlButton,
7
+ GlDropdownItem,
8
+ GlCard,
9
+ GlAlert,
10
+ GlFormInputGroup,
11
+ GlFormTextarea,
12
+ GlForm,
13
+ GlExperimentBadge,
14
+ GlSafeHtmlDirective as SafeHtml,
15
+ } from '@gitlab/ui';
16
+
17
+ import { translate } from '../../utils/i18n';
18
+
19
+ import {
20
+ badgeTypes,
21
+ badgeTypeValidator,
22
+ CHAT_RESET_MESSAGE,
23
+ CHAT_CLEAR_MESSAGE,
24
+ CHAT_NEW_MESSAGE,
25
+ CHAT_INCLUDE_MESSAGE,
26
+ MESSAGE_MODEL_ROLES,
27
+ } from '../chat/constants';
28
+ import { VIEW_TYPES } from '../chat/components/duo_chat_header/constants';
29
+ import DuoChatLoader from '../chat/components/duo_chat_loader/duo_chat_loader.vue';
30
+ import DuoChatPredefinedPrompts from '../chat/components/duo_chat_predefined_prompts/duo_chat_predefined_prompts.vue';
31
+ import DuoChatConversation from '../chat/components/duo_chat_conversation/duo_chat_conversation.vue';
32
+ import DuoChatHeader from '../chat/components/duo_chat_header/duo_chat_header.vue';
33
+ import DuoChatThreads from '../chat/components/duo_chat_threads/duo_chat_threads.vue';
34
+
35
+ export const i18n = {
36
+ CHAT_DEFAULT_TITLE: translate('AgenticDuoChat.chatDefaultTitle', 'GitLab Duo Chat'),
37
+ CHAT_HISTORY_TITLE: translate('AgenticDuoChat.chatHistoryTitle', 'Chat history'),
38
+ CHAT_DISCLAMER: translate(
39
+ 'AgenticDuoChat.chatDisclamer',
40
+ 'Responses may be inaccurate. Verify before use.'
41
+ ),
42
+ CHAT_EMPTY_STATE_TITLE: translate(
43
+ 'AgenticDuoChat.chatEmptyStateTitle',
44
+ '👋 I am GitLab Duo Chat, your personal AI-powered assistant. How can I help you today?'
45
+ ),
46
+ CHAT_PROMPT_PLACEHOLDER_DEFAULT: translate(
47
+ 'AgenticDuoChat.chatPromptPlaceholderDefault',
48
+ 'GitLab Duo Chat'
49
+ ),
50
+ CHAT_PROMPT_PLACEHOLDER_WITH_COMMANDS: translate(
51
+ 'AgenticDuoChat.chatPromptPlaceholderWithCommands',
52
+ 'Type /help to learn more'
53
+ ),
54
+ CHAT_SUBMIT_LABEL: translate('AgenticDuoChat.chatSubmitLabel', 'Send chat message.'),
55
+ CHAT_CANCEL_LABEL: translate('AgenticDuoChat.chatCancelLabel', 'Cancel'),
56
+ CHAT_DEFAULT_PREDEFINED_PROMPTS: [
57
+ translate(
58
+ 'AgenticDuoChat.chatDefaultPredefinedPromptsChangePassword',
59
+ 'How do I change my password in GitLab?'
60
+ ),
61
+ translate('AgenticDuoChat.chatDefaultPredefinedPromptsForkProject', 'How do I fork a project?'),
62
+ translate(
63
+ 'AgenticDuoChat.chatDefaultPredefinedPromptsCloneRepository',
64
+ 'How do I clone a repository?'
65
+ ),
66
+ translate(
67
+ 'AgenticDuoChat.chatDefaultPredefinedPromptsCreateTemplate',
68
+ 'How do I create a template?'
69
+ ),
70
+ ],
71
+ };
72
+
73
+ const isMessage = (item) => Boolean(item) && item?.role;
74
+ const isSlashCommand = (command) => Boolean(command) && command?.name && command.description;
75
+
76
+ // eslint-disable-next-line unicorn/no-array-callback-reference
77
+ const itemsValidator = (items) => items.every(isMessage);
78
+ // eslint-disable-next-line unicorn/no-array-callback-reference
79
+ const slashCommandsValidator = (commands) => commands.every(isSlashCommand);
80
+
81
+ const isThread = (thread) =>
82
+ typeof thread === 'object' &&
83
+ typeof thread.id === 'string' &&
84
+ typeof thread.lastUpdatedAt === 'string' &&
85
+ typeof thread.createdAt === 'string' &&
86
+ typeof thread.conversationType === 'string' &&
87
+ (thread.title === null || typeof thread.title === 'string');
88
+
89
+ // eslint-disable-next-line unicorn/no-array-callback-reference
90
+ const threadListValidator = (threads) => threads.every(isThread);
91
+
92
+ const localeValidator = (value) => {
93
+ try {
94
+ Intl.getCanonicalLocales(value);
95
+ return true;
96
+ } catch {
97
+ return false;
98
+ }
99
+ };
100
+
101
+ export default {
102
+ name: 'DuoChat',
103
+ components: {
104
+ GlButton,
105
+ GlAlert,
106
+ GlFormInputGroup,
107
+ GlFormTextarea,
108
+ GlForm,
109
+ GlExperimentBadge,
110
+ DuoChatLoader,
111
+ DuoChatPredefinedPrompts,
112
+ DuoChatConversation,
113
+ DuoChatHeader,
114
+ DuoChatThreads,
115
+ GlCard,
116
+ GlDropdownItem,
117
+ VueResizable,
118
+ },
119
+ directives: {
120
+ SafeHtml,
121
+ },
122
+ props: {
123
+ /**
124
+ * Determines if the component should be resizable. When true, it renders inside
125
+ * a `vue-resizable` wrapper; otherwise, a standard `div` is used.
126
+ */
127
+ shouldRenderResizable: {
128
+ type: Boolean,
129
+ required: false,
130
+ default: false,
131
+ },
132
+ /**
133
+ * Defines the dimensions of the chat container when resizable.
134
+ * By default, the height is set to match the height of the browser window,
135
+ * and the width is fixed at 400px. The `top` position is left undefined,
136
+ * allowing it to be dynamically adjusted if needed.
137
+ */
138
+ dimensions: {
139
+ type: Object,
140
+ required: false,
141
+ default: () => ({
142
+ width: undefined,
143
+ height: undefined,
144
+ top: undefined,
145
+ left: undefined,
146
+ maxWidth: undefined,
147
+ minWidth: 400,
148
+ maxHeight: undefined,
149
+ minHeight: 400,
150
+ }),
151
+ },
152
+ /**
153
+ * The title of the chat/feature.
154
+ */
155
+ title: {
156
+ type: String,
157
+ required: false,
158
+ default: i18n.CHAT_DEFAULT_TITLE,
159
+ },
160
+ /**
161
+ * Array of messages to display in the chat.
162
+ */
163
+ messages: {
164
+ type: Array,
165
+ required: false,
166
+ default: () => [],
167
+ validator: itemsValidator,
168
+ },
169
+ /**
170
+ * The ID of the active thread (if any).
171
+ */
172
+ activeThreadId: {
173
+ type: String,
174
+ required: false,
175
+ default: () => '',
176
+ },
177
+ /**
178
+ * The chat page that should be shown.
179
+ */
180
+ multiThreadedView: {
181
+ type: String,
182
+ required: false,
183
+ default: VIEW_TYPES.LIST,
184
+ validator: (value) => [VIEW_TYPES.LIST, VIEW_TYPES.CHAT].includes(value),
185
+ },
186
+ /**
187
+ * Array of RequestIds that have been canceled.
188
+ */
189
+ canceledRequestIds: {
190
+ type: Array,
191
+ required: false,
192
+ default: () => [],
193
+ },
194
+ /**
195
+ * A non-recoverable error message to display in the chat.
196
+ */
197
+ error: {
198
+ type: String,
199
+ required: false,
200
+ default: '',
201
+ },
202
+ /**
203
+ * Array of messages to display in the chat.
204
+ */
205
+ threadList: {
206
+ type: Array,
207
+ required: false,
208
+ default: () => [],
209
+ validator: threadListValidator,
210
+ },
211
+
212
+ /**
213
+ * Whether the chat is currently fetching a response from AI.
214
+ */
215
+ isLoading: {
216
+ type: Boolean,
217
+ required: false,
218
+ default: false,
219
+ },
220
+ /**
221
+ * Whether the conversational interfaces should be enabled.
222
+ */
223
+ isChatAvailable: {
224
+ type: Boolean,
225
+ required: false,
226
+ default: true,
227
+ },
228
+ /**
229
+ * Whether the insertCode feature should be available.
230
+ */
231
+ enableCodeInsertion: {
232
+ type: Boolean,
233
+ required: false,
234
+ default: false,
235
+ },
236
+ /**
237
+ * Array of predefined prompts to display in the chat to start a conversation.
238
+ */
239
+ predefinedPrompts: {
240
+ type: Array,
241
+ required: false,
242
+ default: () => i18n.CHAT_DEFAULT_PREDEFINED_PROMPTS,
243
+ },
244
+ /**
245
+ * The type of the badge. This is passed down to the `GlExperimentBadge` component. Refer that component for more information.
246
+ */
247
+ badgeType: {
248
+ type: String || null,
249
+ required: false,
250
+ default: badgeTypes[0],
251
+ validator: badgeTypeValidator,
252
+ },
253
+ /**
254
+ * The current tool's name to display in the loading message while waiting for a response from AI. Refer the `DuoChatLoader` component for more information.
255
+ */
256
+ toolName: {
257
+ type: String,
258
+ required: false,
259
+ default: i18n.CHAT_DEFAULT_TITLE,
260
+ },
261
+ /**
262
+ * Array of slash commands to display in the chat.
263
+ */
264
+ slashCommands: {
265
+ type: Array,
266
+ required: false,
267
+ default: () => [],
268
+ validator: slashCommandsValidator,
269
+ },
270
+ /**
271
+ * Whether the header should be displayed.
272
+ */
273
+ showHeader: {
274
+ type: Boolean,
275
+ required: false,
276
+ default: true,
277
+ },
278
+ /**
279
+ * Override the default empty state title text.
280
+ */
281
+ emptyStateTitle: {
282
+ type: String,
283
+ required: false,
284
+ default: i18n.CHAT_EMPTY_STATE_TITLE,
285
+ },
286
+ /**
287
+ * Override the default chat prompt placeholder text.
288
+ */
289
+ chatPromptPlaceholder: {
290
+ type: String,
291
+ required: false,
292
+ default: '',
293
+ },
294
+ /**
295
+ * Whether the chat is running in multi-threaded mode
296
+ */
297
+ isMultithreaded: {
298
+ type: Boolean,
299
+ required: false,
300
+ default: false,
301
+ },
302
+ /**
303
+ * The preferred locale for the chat interface.
304
+ * Follows BCP 47 language tag format (e.g., 'en-US', 'fr-FR', 'es-ES').
305
+ */
306
+ preferredLocale: {
307
+ type: Array,
308
+ required: false,
309
+ default: () => ['en-US', 'en'],
310
+ validator: localeValidator,
311
+ },
312
+ },
313
+ data() {
314
+ return {
315
+ isHidden: false,
316
+ prompt: '',
317
+ scrolledToBottom: true,
318
+ activeCommandIndex: 0,
319
+ displaySubmitButton: true,
320
+ compositionJustEnded: false,
321
+ contextItemsMenuIsOpen: false,
322
+ contextItemMenuRef: null,
323
+ currentView: this.multiThreadedView,
324
+ };
325
+ },
326
+ computed: {
327
+ shouldShowThreadList() {
328
+ return this.isMultithreaded && this.currentView === VIEW_TYPES.LIST;
329
+ },
330
+ withSlashCommands() {
331
+ return this.slashCommands.length > 0;
332
+ },
333
+ hasMessages() {
334
+ return this.messages?.length > 0;
335
+ },
336
+ conversations() {
337
+ if (!this.hasMessages) return [];
338
+
339
+ return this.messages.reduce(
340
+ (acc, message) => {
341
+ if (message.content === CHAT_RESET_MESSAGE) {
342
+ acc.push([]);
343
+ } else {
344
+ acc[acc.length - 1].push(message);
345
+ }
346
+ return acc;
347
+ },
348
+ [[]]
349
+ );
350
+ },
351
+ lastMessage() {
352
+ return this.messages?.[this.messages.length - 1];
353
+ },
354
+ caseInsensitivePrompt() {
355
+ return this.prompt.toLowerCase().trim();
356
+ },
357
+ isStreaming() {
358
+ if (this.canceledRequestIds.includes(this.lastMessage?.requestId)) {
359
+ return false;
360
+ }
361
+ return Boolean(
362
+ (this.lastMessage?.chunks?.length > 0 && !this.lastMessage?.content) ||
363
+ typeof this.lastMessage?.chunkId === 'number'
364
+ );
365
+ },
366
+ filteredSlashCommands() {
367
+ return this.slashCommands
368
+ .filter((c) => c.name.toLowerCase().startsWith(this.caseInsensitivePrompt))
369
+ .filter((c) => {
370
+ if (c.name === CHAT_INCLUDE_MESSAGE) {
371
+ return this.hasContextItemSelectionMenu;
372
+ }
373
+ return true;
374
+ });
375
+ },
376
+ shouldShowSlashCommands() {
377
+ if (!this.withSlashCommands || this.contextItemsMenuIsOpen) return false;
378
+ const startsWithSlash = this.caseInsensitivePrompt.startsWith('/');
379
+ const startsWithSlashCommand = this.slashCommands.some((c) =>
380
+ this.caseInsensitivePrompt.startsWith(c.name)
381
+ );
382
+ return startsWithSlash && this.filteredSlashCommands.length && !startsWithSlashCommand;
383
+ },
384
+ shouldShowContextItemSelectionMenu() {
385
+ if (!this.hasContextItemSelectionMenu) {
386
+ return false;
387
+ }
388
+
389
+ const isSlash = this.caseInsensitivePrompt === '/';
390
+ if (!this.caseInsensitivePrompt || isSlash) {
391
+ // if user has removed entire command (or whole command except for '/') we should close context item menu and allow slash command menu to show again
392
+ return false;
393
+ }
394
+
395
+ return CHAT_INCLUDE_MESSAGE.startsWith(this.caseInsensitivePrompt);
396
+ },
397
+ inputPlaceholder() {
398
+ if (this.chatPromptPlaceholder) {
399
+ return this.chatPromptPlaceholder;
400
+ }
401
+
402
+ return this.withSlashCommands
403
+ ? i18n.CHAT_PROMPT_PLACEHOLDER_WITH_COMMANDS
404
+ : i18n.CHAT_PROMPT_PLACEHOLDER_DEFAULT;
405
+ },
406
+ hasContextItemSelectionMenu() {
407
+ return Boolean(this.contextItemMenuRef);
408
+ },
409
+ activeThread() {
410
+ return this.activeThreadId
411
+ ? this.threadList.find((thread) => thread.id === this.activeThreadId)
412
+ : null;
413
+ },
414
+ activeThreadTitle() {
415
+ return this.activeThread?.title;
416
+ },
417
+ activeThreadTitleForView() {
418
+ return (this.currentView === VIEW_TYPES.CHAT && this.activeThreadTitle) || '';
419
+ },
420
+ },
421
+ watch: {
422
+ multiThreadedView(newView) {
423
+ this.currentView = newView;
424
+ },
425
+ isLoading(newVal) {
426
+ if (!newVal && !this.isStreaming) {
427
+ this.displaySubmitButton = true; // Re-enable submit button when loading stops
428
+ }
429
+ this.isHidden = false;
430
+ },
431
+ isStreaming(newVal) {
432
+ if (!newVal && !this.isLoading) {
433
+ this.displaySubmitButton = true; // Re-enable submit button when streaming stops
434
+ }
435
+ },
436
+ lastMessage(newMessage) {
437
+ if (this.scrolledToBottom || newMessage?.role.toLowerCase() === MESSAGE_MODEL_ROLES.user) {
438
+ // only scroll to bottom on new message if the user hasn't explicitly scrolled up to view an earlier message
439
+ // or if the user has just submitted a new message
440
+ this.scrollToBottom();
441
+ }
442
+ },
443
+ shouldShowSlashCommands(shouldShow) {
444
+ if (shouldShow) {
445
+ this.onShowSlashCommands();
446
+ }
447
+ },
448
+ },
449
+ created() {
450
+ this.handleScrollingThrottled = throttle(this.handleScrolling, 200); // Assume a 200ms throttle for example
451
+ },
452
+ mounted() {
453
+ this.scrollToBottom();
454
+ },
455
+
456
+ methods: {
457
+ onGoBack() {
458
+ this.$emit('back-to-list');
459
+ },
460
+ onNewChat() {
461
+ this.$emit('new-chat');
462
+
463
+ this.$nextTick(() => {
464
+ this.focusChatInput();
465
+ });
466
+ },
467
+ updateSize(e) {
468
+ this.$emit('chat-resize', e);
469
+ },
470
+ compositionEnd() {
471
+ this.compositionJustEnded = true;
472
+ },
473
+ hideChat() {
474
+ this.isHidden = true;
475
+ /**
476
+ * Emitted when clicking the cross in the title and the chat gets closed.
477
+ */
478
+ this.$emit('chat-hidden');
479
+ },
480
+ cancelPrompt() {
481
+ /**
482
+ * Emitted when user clicks the stop button in the textarea
483
+ */
484
+
485
+ this.displaySubmitButton = true;
486
+ this.$emit('chat-cancel');
487
+ this.setPromptAndFocus();
488
+ },
489
+ sendChatPrompt() {
490
+ if (!this.displaySubmitButton || this.contextItemsMenuIsOpen) {
491
+ return;
492
+ }
493
+ if (this.prompt) {
494
+ if (
495
+ this.caseInsensitivePrompt.startsWith(CHAT_INCLUDE_MESSAGE) &&
496
+ this.hasContextItemSelectionMenu
497
+ ) {
498
+ this.contextItemsMenuIsOpen = true;
499
+ return;
500
+ }
501
+
502
+ if (
503
+ ![CHAT_RESET_MESSAGE, CHAT_CLEAR_MESSAGE, CHAT_NEW_MESSAGE].includes(
504
+ this.caseInsensitivePrompt
505
+ )
506
+ ) {
507
+ this.displaySubmitButton = false;
508
+ }
509
+
510
+ /**
511
+ * Emitted when a new user prompt should be sent out.
512
+ *
513
+ * @param {String} prompt The user prompt to send.
514
+ */
515
+ this.$emit('send-chat-prompt', this.prompt.trim());
516
+
517
+ this.setPromptAndFocus();
518
+ }
519
+ },
520
+ sendPredefinedPrompt(prompt) {
521
+ this.contextItemsMenuIsOpen = false;
522
+ this.prompt = prompt;
523
+ this.sendChatPrompt();
524
+ },
525
+ handleScrolling(event) {
526
+ const { scrollTop, offsetHeight, scrollHeight } = event.target;
527
+ this.scrolledToBottom = scrollTop + offsetHeight >= scrollHeight;
528
+ },
529
+ async scrollToBottom() {
530
+ await this.$nextTick();
531
+
532
+ this.$refs.anchor?.scrollIntoView?.();
533
+ },
534
+ focusChatInput() {
535
+ // This method is also called directly by consumers of this component
536
+ // https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/blob/dae2d4669ab4da327921492a2962beae8a05c290/webviews/vue2/gitlab_duo_chat/src/App.vue#L109
537
+ this.$refs.prompt?.$el?.focus();
538
+ },
539
+ onTrackFeedback(event) {
540
+ /**
541
+ * Notify listeners about the feedback form submission on a response message.
542
+ * @param {*} event An event, containing the feedback choices and the extended feedback text.
543
+ */
544
+ this.$emit('track-feedback', event);
545
+ },
546
+ onShowSlashCommands() {
547
+ /**
548
+ * Emitted when user opens the slash commands menu
549
+ */
550
+ this.$emit('chat-slash');
551
+ },
552
+ sendChatPromptOnEnter(e) {
553
+ const { metaKey, ctrlKey, altKey, shiftKey, isComposing } = e;
554
+ const isModifierKey = metaKey || ctrlKey || altKey || shiftKey;
555
+
556
+ return !(isModifierKey || isComposing || this.compositionJustEnded);
557
+ },
558
+ onInputKeyup(e) {
559
+ const { key } = e;
560
+
561
+ if (this.contextItemsMenuIsOpen) {
562
+ if (!this.shouldShowContextItemSelectionMenu) {
563
+ this.contextItemsMenuIsOpen = false;
564
+ }
565
+ this.contextItemMenuRef?.handleKeyUp(e);
566
+ return;
567
+ }
568
+ if (this.caseInsensitivePrompt === CHAT_INCLUDE_MESSAGE) {
569
+ this.contextItemsMenuIsOpen = true;
570
+ return;
571
+ }
572
+
573
+ if (this.shouldShowSlashCommands) {
574
+ e.preventDefault();
575
+
576
+ if (key === 'Enter') {
577
+ this.selectSlashCommand(this.activeCommandIndex);
578
+ } else if (key === 'ArrowUp') {
579
+ this.prevCommand();
580
+ } else if (key === 'ArrowDown') {
581
+ this.nextCommand();
582
+ } else {
583
+ this.activeCommandIndex = 0;
584
+ }
585
+ } else if (key === 'Enter' && this.sendChatPromptOnEnter(e)) {
586
+ e.preventDefault();
587
+
588
+ this.sendChatPrompt();
589
+ }
590
+
591
+ this.compositionJustEnded = false;
592
+ },
593
+ prevCommand() {
594
+ this.activeCommandIndex -= 1;
595
+ this.wrapCommandIndex();
596
+ },
597
+ nextCommand() {
598
+ this.activeCommandIndex += 1;
599
+ this.wrapCommandIndex();
600
+ },
601
+ wrapCommandIndex() {
602
+ if (this.activeCommandIndex < 0) {
603
+ this.activeCommandIndex = this.filteredSlashCommands.length - 1;
604
+ } else if (this.activeCommandIndex >= this.filteredSlashCommands.length) {
605
+ this.activeCommandIndex = 0;
606
+ }
607
+ },
608
+ async setPromptAndFocus(prompt = '') {
609
+ this.prompt = prompt;
610
+ await this.$nextTick();
611
+ this.focusChatInput();
612
+ },
613
+ selectSlashCommand(index) {
614
+ const command = this.filteredSlashCommands[index];
615
+ if (command.shouldSubmit) {
616
+ this.prompt = command.name;
617
+ this.sendChatPrompt();
618
+ } else {
619
+ this.setPromptAndFocus(`${command.name} `);
620
+
621
+ if (command.name === CHAT_INCLUDE_MESSAGE && this.hasContextItemSelectionMenu) {
622
+ this.contextItemsMenuIsOpen = true;
623
+ }
624
+ }
625
+ },
626
+ onInsertCodeSnippet(e) {
627
+ /**
628
+ * Emit insert-code-snippet event that clients can use to interact with a suggested code.
629
+ * @param {*} event An event containing code string in the "detail.code" field.
630
+ */
631
+ this.$emit('insert-code-snippet', e);
632
+ },
633
+ onCopyCodeSnippet(e) {
634
+ /**
635
+ * Emit copy-code-snippet event that clients can use to interact with a suggested code.
636
+ * @param {*} event An event containing code string in the "detail.code" field.
637
+ */
638
+ this.$emit('copy-code-snippet', e);
639
+ },
640
+ onCopyMessage(e) {
641
+ /**
642
+ * Emit copy-message event that clients can use to copy chat message content.
643
+ * @param {*} event An event containing code string in the "detail.message" field.
644
+ */
645
+ this.$emit('copy-message', e);
646
+ },
647
+ onGetContextItemContent(event) {
648
+ /**
649
+ * Emit get-context-item-content event that tells clients to load the full file content for a selected context item.
650
+ * The fully hydrated context item should be updated in the chat message context item.
651
+ * @param {*} event An event containing the message ID and context item to hydrate
652
+ */
653
+ this.$emit('get-context-item-content', event);
654
+ },
655
+ closeContextItemsMenuOpen() {
656
+ this.contextItemsMenuIsOpen = false;
657
+ this.setPromptAndFocus();
658
+ },
659
+ setContextItemsMenuRef(ref) {
660
+ this.contextItemMenuRef = ref;
661
+ },
662
+ onSelectThread(thread) {
663
+ /**
664
+ * Emitted when a thread is selected from the history.
665
+ * @param {Object} thread The selected thread object
666
+ */
667
+ this.$emit('thread-selected', thread);
668
+ },
669
+ onDeleteThread(threadId) {
670
+ /**
671
+ * Emitted when a thread is deleted from the history.
672
+ * @param {String} threadId The ID of the thread to delete
673
+ */
674
+ this.$emit('delete-thread', threadId);
675
+ },
676
+ },
677
+ i18n,
678
+ };
679
+ </script>
680
+ <template>
681
+ <component
682
+ :is="shouldRenderResizable ? 'vue-resizable' : 'div'"
683
+ :width="shouldRenderResizable ? dimensions.width : null"
684
+ :height="shouldRenderResizable ? dimensions.height : null"
685
+ :max-width="shouldRenderResizable ? dimensions.maxWidth : null"
686
+ :max-height="shouldRenderResizable ? dimensions.maxHeight : null"
687
+ :min-width="shouldRenderResizable ? dimensions.minWidth : null"
688
+ :left="shouldRenderResizable ? dimensions.left : null"
689
+ :top="shouldRenderResizable ? dimensions.top : null"
690
+ :fit-parent="true"
691
+ :min-height="shouldRenderResizable ? dimensions.minHeight : null"
692
+ :class="{
693
+ 'duo-chat-resizable': shouldRenderResizable,
694
+ 'non-resizable-wrapper': !shouldRenderResizable,
695
+ }"
696
+ :active="shouldRenderResizable ? ['l', 't', 'lt'] : null"
697
+ @resize:end="updateSize"
698
+ >
699
+ <aside
700
+ v-if="!isHidden"
701
+ id="chat-component"
702
+ :class="{
703
+ 'resizable-content': shouldRenderResizable,
704
+ 'duo-chat-drawer': !shouldRenderResizable,
705
+ }"
706
+ class="markdown-code-block duo-chat gl-bottom-0 gl-max-h-full gl-flex gl-flex-col"
707
+ role="complementary"
708
+ data-testid="chat-component"
709
+ >
710
+ <duo-chat-header
711
+ v-if="showHeader"
712
+ ref="header"
713
+ :active-thread-id="activeThreadId"
714
+ :title="
715
+ isMultithreaded && currentView === 'list' ? $options.i18n.CHAT_HISTORY_TITLE : title
716
+ "
717
+ :subtitle="activeThreadTitleForView"
718
+ :error="error"
719
+ :is-multithreaded="isMultithreaded"
720
+ :current-view="currentView"
721
+ :should-render-resizable="shouldRenderResizable"
722
+ :badge-type="isMultithreaded ? null : badgeType"
723
+ @go-back="onGoBack"
724
+ @new-chat="onNewChat"
725
+ @close="hideChat"
726
+ >
727
+ <template #subheader>
728
+ <slot name="subheader"></slot>
729
+ </template>
730
+ </duo-chat-header>
731
+
732
+ <div
733
+ class="gl-overflow-y-auto gl-flex gl-flex-col gl-flex-1 gl-flex-grow gl-bg-inherit gl-overscroll-contain"
734
+ data-testid="chat-history"
735
+ @scroll="handleScrollingThrottled"
736
+ >
737
+ <duo-chat-threads
738
+ v-if="shouldShowThreadList"
739
+ :threads="threadList"
740
+ :preferred-locale="preferredLocale"
741
+ @new-chat="onNewChat"
742
+ @select-thread="onSelectThread"
743
+ @delete-thread="onDeleteThread"
744
+ @close="hideChat"
745
+ />
746
+ <transition-group
747
+ v-else
748
+ mode="out-in"
749
+ tag="section"
750
+ name="message"
751
+ class="duo-chat-history gl-p-5 gl-mt-auto"
752
+ >
753
+ <duo-chat-conversation
754
+ v-for="(conversation, index) in conversations"
755
+ :key="`conversation-${index}`"
756
+ :enable-code-insertion="enableCodeInsertion"
757
+ :messages="conversation"
758
+ :canceled-request-ids="canceledRequestIds"
759
+ :show-delimiter="index > 0"
760
+ @track-feedback="onTrackFeedback"
761
+ @insert-code-snippet="onInsertCodeSnippet"
762
+ @copy-code-snippet="onCopyCodeSnippet"
763
+ @copy-message="onCopyMessage"
764
+ @get-context-item-content="onGetContextItemContent"
765
+ />
766
+ <template v-if="!hasMessages && !isLoading">
767
+ <div
768
+ key="empty-state-message"
769
+ class="duo-chat-message gl-rounded-bl-none gl-border-1 gl-border-solid gl-border-gray-50 gl-bg-gray-10 gl-p-4 gl-leading-20 gl-text-gray-900 gl-break-anywhere"
770
+ data-testid="gl-duo-chat-empty-state"
771
+ >
772
+ <p v-if="emptyStateTitle" data-testid="gl-duo-chat-empty-state-title" class="gl-m-0">
773
+ {{ emptyStateTitle }}
774
+ </p>
775
+ <duo-chat-predefined-prompts
776
+ key="predefined-prompts"
777
+ :prompts="predefinedPrompts"
778
+ @click="sendPredefinedPrompt"
779
+ />
780
+ </div>
781
+ </template>
782
+ <duo-chat-loader v-if="isLoading" key="loader" :tool-name="toolName" />
783
+ <div key="anchor" ref="anchor" class="scroll-anchor"></div>
784
+ </transition-group>
785
+ </div>
786
+ <footer
787
+ v-if="isChatAvailable && !shouldShowThreadList"
788
+ data-testid="chat-footer"
789
+ class="duo-chat-drawer-footer gl-border-0 gl-bg-default gl-pb-3 gl-shrink-0 gl-relative gl-z-2"
790
+ :class="{ 'duo-chat-drawer-body-scrim-on-footer': !scrolledToBottom }"
791
+ >
792
+ <gl-form data-testid="chat-prompt-form" @submit.stop.prevent="sendChatPrompt">
793
+ <div class="gl-relative gl-max-w-full">
794
+ <!--
795
+ @slot For integrating `<gl-context-items-menu>` component if pinned-context should be available. The following scopedSlot properties are provided: `isOpen`, `onClose`, `setRef`, `focusPrompt`, which should be passed to the `<gl-context-items-menu>` component when rendering, e.g. `<template #context-items-menu="{ isOpen, onClose, setRef, focusPrompt }">` `<duo-chat-context-item-menu :ref="setRef" :open="isOpen" @close="onClose" @focus-prompt="focusPrompt" ...`
796
+ -->
797
+ <slot
798
+ name="context-items-menu"
799
+ :is-open="contextItemsMenuIsOpen"
800
+ :on-close="closeContextItemsMenuOpen"
801
+ :set-ref="setContextItemsMenuRef"
802
+ :focus-prompt="focusChatInput"
803
+ ></slot>
804
+ </div>
805
+
806
+ <gl-form-input-group>
807
+ <div
808
+ class="duo-chat-input gl-min-h-8 gl-max-w-full gl-grow gl-align-top"
809
+ :data-value="prompt"
810
+ >
811
+ <gl-card
812
+ v-if="shouldShowSlashCommands"
813
+ ref="commands"
814
+ class="slash-commands !gl-absolute gl-w-full -gl-translate-y-full gl-list-none gl-pl-0 gl-shadow-md"
815
+ body-class="!gl-p-2"
816
+ >
817
+ <gl-dropdown-item
818
+ v-for="(command, index) in filteredSlashCommands"
819
+ :key="command.name"
820
+ :class="{ 'active-command': index === activeCommandIndex }"
821
+ @mouseenter.native="activeCommandIndex = index"
822
+ @click="selectSlashCommand(index)"
823
+ >
824
+ <span class="gl-flex gl-justify-between">
825
+ <span class="gl-block">{{ command.name }}</span>
826
+ <small class="gl-pl-3 gl-text-right gl-italic gl-text-subtle">{{
827
+ command.description
828
+ }}</small>
829
+ </span>
830
+ </gl-dropdown-item>
831
+ </gl-card>
832
+
833
+ <gl-form-textarea
834
+ ref="prompt"
835
+ v-model="prompt"
836
+ data-testid="chat-prompt-input"
837
+ class="gl-absolute !gl-h-full gl-rounded-br-none gl-rounded-tr-none !gl-bg-transparent !gl-py-4 !gl-shadow-none"
838
+ :class="{ 'gl-truncate': !prompt }"
839
+ :placeholder="inputPlaceholder"
840
+ autofocus
841
+ @keydown.enter.exact.native.prevent
842
+ @keyup.native="onInputKeyup"
843
+ @compositionend="compositionEnd"
844
+ />
845
+ </div>
846
+ <template #append>
847
+ <gl-button
848
+ v-if="displaySubmitButton"
849
+ icon="paper-airplane"
850
+ category="primary"
851
+ variant="confirm"
852
+ class="!gl-absolute gl-bottom-2 gl-right-2 !gl-rounded-full"
853
+ type="submit"
854
+ data-testid="chat-prompt-submit-button"
855
+ :aria-label="$options.i18n.CHAT_SUBMIT_LABEL"
856
+ />
857
+ <gl-button
858
+ v-else
859
+ icon="stop"
860
+ category="primary"
861
+ variant="default"
862
+ class="!gl-absolute gl-bottom-2 gl-right-2 !gl-rounded-full"
863
+ data-testid="chat-prompt-cancel-button"
864
+ :aria-label="$options.i18n.CHAT_CANCEL_LABEL"
865
+ @click="cancelPrompt"
866
+ />
867
+ </template>
868
+ </gl-form-input-group>
869
+ </gl-form>
870
+ <p class="gl-mb-0 gl-mt-3 gl-px-4 gl-text-sm gl-text-secondary">
871
+ {{ $options.i18n.CHAT_DISCLAMER }}
872
+ </p>
873
+ </footer>
874
+ </aside>
875
+ </component>
876
+ </template>