forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotChatUserMessage.vue
More file actions
322 lines (297 loc) · 9.52 KB
/
Copy pathCopilotChatUserMessage.vue
File metadata and controls
322 lines (297 loc) · 9.52 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
<script setup lang="ts">
import { computed, getCurrentInstance, onBeforeUnmount, ref } from "vue";
import type { UserMessage } from "@ag-ui/core";
import { useCopilotChatConfiguration } from "../../providers/useCopilotChatConfiguration";
import { CopilotChatDefaultLabels } from "../../providers/types";
import {
IconCheck,
IconChevronLeft,
IconChevronRight,
IconCopy,
IconEdit,
} from "../icons";
import type {
CopilotChatUserMessageBranchNavigationSlotProps,
CopilotChatUserMessageCopyButtonSlotProps,
CopilotChatUserMessageEditButtonSlotProps,
CopilotChatUserMessageLayoutSlotProps,
CopilotChatUserMessageMessageRendererSlotProps,
CopilotChatUserMessageOnEditMessageProps,
CopilotChatUserMessageOnSwitchToBranchProps,
CopilotChatUserMessageToolbarSlotProps,
} from "./types";
const props = withDefaults(
defineProps<{
message: UserMessage;
branchIndex?: number;
numberOfBranches?: number;
}>(),
{
branchIndex: 0,
numberOfBranches: 1,
},
);
defineSlots<{
"message-renderer"?: (
props: CopilotChatUserMessageMessageRendererSlotProps,
) => unknown;
toolbar?: (props: CopilotChatUserMessageToolbarSlotProps) => unknown;
"copy-button"?: (props: CopilotChatUserMessageCopyButtonSlotProps) => unknown;
"edit-button"?: (props: CopilotChatUserMessageEditButtonSlotProps) => unknown;
"branch-navigation"?: (
props: CopilotChatUserMessageBranchNavigationSlotProps,
) => unknown;
layout?: (props: CopilotChatUserMessageLayoutSlotProps) => unknown;
"toolbar-items"?: () => unknown;
}>();
const emit = defineEmits<{
"edit-message": [payload: CopilotChatUserMessageOnEditMessageProps];
"switch-to-branch": [payload: CopilotChatUserMessageOnSwitchToBranchProps];
}>();
const config = useCopilotChatConfiguration();
const labels = computed(() => config.value?.labels ?? CopilotChatDefaultLabels);
const instance = getCurrentInstance();
const copied = ref(false);
let copiedResetTimeout: ReturnType<typeof setTimeout> | null = null;
const vnodeProps = computed(
() => (instance?.vnode.props ?? {}) as Record<string, unknown>,
);
const toolbarButtonClass = [
"cpk:inline-flex cpk:h-8 cpk:w-8 cpk:items-center cpk:justify-center cpk:rounded-md cpk:p-0",
"cpk:cursor-pointer cpk:text-[rgb(93,93,93)] cpk:transition-colors cpk:hover:bg-[#E8E8E8]",
"cpk:hover:text-[rgb(93,93,93)] cpk:dark:text-[rgb(243,243,243)] cpk:dark:hover:bg-[#303030]",
"cpk:dark:hover:text-[rgb(243,243,243)] cpk:disabled:pointer-events-none cpk:disabled:opacity-50",
].join(" ");
function flattenUserMessageContent(content?: UserMessage["content"]): string {
if (!content) {
return "";
}
if (typeof content === "string") {
return content;
}
return content
.map((part) => {
if (
part &&
typeof part === "object" &&
"type" in part &&
(part as { type?: unknown }).type === "text" &&
typeof (part as { text?: unknown }).text === "string"
) {
return (part as { text: string }).text;
}
return "";
})
.filter((text) => text.length > 0)
.join("\n");
}
const flattenedContent = computed(() =>
flattenUserMessageContent(props.message.content),
);
const isMultiline = computed(() => flattenedContent.value.includes("\n"));
function hasListener(listenerName: string) {
const listener = vnodeProps.value[listenerName];
if (Array.isArray(listener)) {
return listener.length > 0;
}
return !!listener;
}
const hasEditAction = computed(() => hasListener("onEditMessage"));
const showBranchNavigation = computed(
() => props.numberOfBranches > 1 && hasListener("onSwitchToBranch"),
);
const canGoPrev = computed(() => props.branchIndex > 0);
const canGoNext = computed(
() => props.branchIndex < props.numberOfBranches - 1,
);
function resetCopiedStateWithDelay() {
if (copiedResetTimeout) {
clearTimeout(copiedResetTimeout);
}
copied.value = true;
copiedResetTimeout = setTimeout(() => {
copied.value = false;
copiedResetTimeout = null;
}, 2000);
}
async function handleCopyMessage() {
if (!flattenedContent.value) return;
if (
typeof navigator === "undefined" ||
typeof navigator.clipboard?.writeText !== "function"
) {
return;
}
try {
await navigator.clipboard.writeText(flattenedContent.value);
resetCopiedStateWithDelay();
} catch (error) {
console.error("Failed to copy to clipboard:", error);
}
}
function handleEditMessage() {
if (!hasEditAction.value) {
return;
}
const payload = { message: props.message };
emit("edit-message", payload);
}
function switchToBranch(branchIndex: number) {
if (!showBranchNavigation.value) {
return;
}
const payload = {
branchIndex,
numberOfBranches: props.numberOfBranches,
message: props.message,
};
emit("switch-to-branch", payload);
}
function goPrev() {
if (!canGoPrev.value) {
return;
}
switchToBranch(props.branchIndex - 1);
}
function goNext() {
if (!canGoNext.value) {
return;
}
switchToBranch(props.branchIndex + 1);
}
onBeforeUnmount(() => {
if (copiedResetTimeout) {
clearTimeout(copiedResetTimeout);
}
});
</script>
<template>
<slot
name="layout"
:message="message"
:content="flattenedContent"
:is-multiline="isMultiline"
:show-branch-navigation="showBranchNavigation"
:has-edit-action="hasEditAction"
:branch-index="branchIndex"
:number-of-branches="numberOfBranches"
:can-go-prev="canGoPrev"
:can-go-next="canGoNext"
:on-copy="handleCopyMessage"
:on-edit="handleEditMessage"
:go-prev="goPrev"
:go-next="goNext"
:copied="copied"
>
<div
data-copilotkit
data-testid="copilot-user-message"
class="cpk:flex cpk:flex-col cpk:items-end cpk:group cpk:pt-10"
:data-message-id="message.id"
v-bind="$attrs"
>
<slot
name="message-renderer"
:message="message"
:content="flattenedContent"
:is-multiline="isMultiline"
>
<div
class="cpk:prose cpk:dark:prose-invert cpk:bg-muted cpk:relative cpk:max-w-[80%] cpk:rounded-[18px] cpk:px-4 cpk:py-1.5 cpk:inline-block cpk:whitespace-pre-wrap"
:data-multiline="isMultiline ? 'true' : undefined"
:class="{ 'cpk:py-3': isMultiline }"
>
{{ flattenedContent }}
</div>
</slot>
<slot
name="toolbar"
:message="message"
:show-branch-navigation="showBranchNavigation"
:has-edit-action="hasEditAction"
>
<div
class="cpk:w-full cpk:bg-transparent cpk:flex cpk:items-center cpk:justify-end cpk:-mr-[5px] cpk:mt-[4px] cpk:invisible cpk:group-hover:visible"
>
<div class="cpk:flex cpk:items-center cpk:gap-1 cpk:justify-end">
<slot name="toolbar-items" />
<slot
name="copy-button"
:on-copy="handleCopyMessage"
:copied="copied"
:label="labels.userMessageToolbarCopyMessageLabel"
>
<button
data-testid="copilot-user-copy-button"
type="button"
:class="toolbarButtonClass"
:aria-label="labels.userMessageToolbarCopyMessageLabel"
:title="labels.userMessageToolbarCopyMessageLabel"
@click="handleCopyMessage"
>
<IconCheck v-if="copied" class="cpk:size-[18px]" />
<IconCopy v-else class="cpk:size-[18px]" />
</button>
</slot>
<slot
v-if="hasEditAction"
name="edit-button"
:on-edit="handleEditMessage"
:label="labels.userMessageToolbarEditMessageLabel"
>
<button
type="button"
:class="toolbarButtonClass"
:aria-label="labels.userMessageToolbarEditMessageLabel"
:title="labels.userMessageToolbarEditMessageLabel"
@click="handleEditMessage"
>
<IconEdit class="cpk:size-[18px]" />
</button>
</slot>
<slot
v-if="showBranchNavigation"
name="branch-navigation"
:branch-index="branchIndex"
:number-of-branches="numberOfBranches"
:can-go-prev="canGoPrev"
:can-go-next="canGoNext"
:go-prev="goPrev"
:go-next="goNext"
>
<div class="cpk:flex cpk:items-center cpk:gap-1">
<button
type="button"
:class="toolbarButtonClass"
class="cpk:h-6 cpk:w-6 cpk:p-0"
:disabled="!canGoPrev"
aria-label="Previous branch"
title="Previous branch"
@click="goPrev"
>
<IconChevronLeft class="cpk:size-[20px]" />
</button>
<span
class="cpk:text-sm cpk:text-muted-foreground cpk:px-0 cpk:font-medium"
>
{{ branchIndex + 1 }}/{{ numberOfBranches }}
</span>
<button
type="button"
:class="toolbarButtonClass"
class="cpk:h-6 cpk:w-6 cpk:p-0"
:disabled="!canGoNext"
aria-label="Next branch"
title="Next branch"
@click="goNext"
>
<IconChevronRight class="cpk:size-[20px]" />
</button>
</div>
</slot>
</div>
</div>
</slot>
</div>
</slot>
</template>