forked from nuxt/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatMessage.vue
More file actions
146 lines (133 loc) · 6.26 KB
/
Copy pathChatMessage.vue
File metadata and controls
146 lines (133 loc) · 6.26 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
<script lang="ts">
import type { VNode } from 'vue'
import type { AppConfig } from '@nuxt/schema'
import type { UIDataTypes, UIMessage, UITools, FileUIPart, TextUIPart } from 'ai'
import theme from '#build/ui/chat-message'
import type { AvatarProps, ButtonProps, IconProps } from '../types'
import type { ComponentConfig } from '../types/tv'
type ChatMessage = ComponentConfig<typeof theme, AppConfig, 'chatMessage'>
export interface ChatMessageProps<TMetadata = unknown, TDataParts extends UIDataTypes = UIDataTypes, TTools extends UITools = UITools> extends UIMessage<TMetadata, TDataParts, TTools> {
/**
* The element or component this component should render as.
* @defaultValue 'article'
*/
as?: any
/**
* @IconifyIcon
*/
icon?: IconProps['name']
avatar?: AvatarProps & { [key: string]: any }
/**
* @defaultValue 'naked'
*/
variant?: ChatMessage['variants']['variant']
/**
* @defaultValue 'neutral'
*/
color?: ChatMessage['variants']['color']
/**
* @defaultValue 'left'
*/
side?: ChatMessage['variants']['side']
/**
* Display a list of actions under the message.
* The `label` will be used in a tooltip.
* `{ size: 'xs', color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
*/
actions?: (Omit<ButtonProps, 'onClick'> & { onClick?: (e: MouseEvent, message: UIMessage<TMetadata, TDataParts, TTools>) => void })[]
/**
* Render the message in a compact style.
* This is done automatically when used inside a `UChatPalette`{lang="ts-type"}.
* @defaultValue false
*/
compact?: boolean
/**
* @deprecated Use `parts` instead. (https://ai-sdk.dev/docs/migration-guides/migration-guide-5-0#content--parts-array)
* Use to display the content of the message.
*/
content?: string
class?: any
ui?: ChatMessage['slots']
}
export interface ChatMessageSlots<TMetadata = unknown, TDataParts extends UIDataTypes = UIDataTypes, TTools extends UITools = UITools> {
header?(props: UIMessage<TMetadata, TDataParts, TTools>): VNode[]
leading?(props: UIMessage<TMetadata, TDataParts, TTools> & { avatar: ChatMessageProps<TMetadata, TDataParts, TTools>['avatar'], ui: ChatMessage['ui'] }): VNode[]
files?(props: Omit<UIMessage<TMetadata, TDataParts, TTools>, 'parts'> & { parts: FileUIPart[] }): VNode[]
content?(props: UIMessage<TMetadata, TDataParts, TTools> & { content?: string }): VNode[]
actions?(props: UIMessage<TMetadata, TDataParts, TTools> & { actions: ChatMessageProps<TMetadata, TDataParts, TTools>['actions'] }): VNode[]
}
</script>
<script setup lang="ts" generic="TMetadata, TDataParts extends UIDataTypes, TTools extends UITools">
import { computed } from 'vue'
import { Primitive } from 'reka-ui'
import { useAppConfig } from '#imports'
import { useComponentUI } from '../composables/useComponentUI'
import { omit } from '../utils'
import { tv } from '../utils/tv'
import UButton from './Button.vue'
import UTooltip from './Tooltip.vue'
import UAvatar from './Avatar.vue'
import UIcon from './Icon.vue'
const props = withDefaults(defineProps<ChatMessageProps<TMetadata, TDataParts, TTools>>(), {
as: 'article'
})
const slots = defineSlots<ChatMessageSlots<TMetadata, TDataParts, TTools>>()
const appConfig = useAppConfig() as ChatMessage['AppConfig']
const uiProp = useComponentUI('chatMessage', props)
const fileParts = computed(() => props.parts?.filter((part): part is FileUIPart => part.type === 'file') ?? [])
const textParts = computed(() => props.parts?.filter((part): part is TextUIPart => part.type === 'text') ?? [])
const messageProps = computed(() => omit(props, ['as', 'icon', 'avatar', 'variant', 'side', 'actions', 'compact', 'class', 'ui', 'content']))
const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.chatMessage || {}) })({
variant: props.variant,
color: props.color,
side: props.side,
leading: !!props.icon || !!props.avatar || !!slots.leading,
actions: !!props.actions || !!slots.actions,
compact: props.compact
}))
</script>
<template>
<Primitive :as="as" :data-role="role" data-slot="root" :class="ui.root({ class: [uiProp?.root, props.class] })">
<div v-if="(!!slots.files && fileParts.length) || !!slots.header" data-slot="header" :class="ui.header({ class: uiProp?.header })">
<slot name="header" v-bind="{ ...messageProps }">
<div v-if="!!slots.files && fileParts.length" data-slot="files" :class="ui.files({ class: uiProp?.files })">
<slot name="files" v-bind="{ ...messageProps, parts: fileParts }" />
</div>
</slot>
</div>
<div data-slot="container" :class="ui.container({ class: uiProp?.container })">
<div v-if="icon || avatar || !!slots.leading" data-slot="leading" :class="ui.leading({ class: uiProp?.leading })">
<slot name="leading" v-bind="{ ...messageProps, avatar, ui }">
<UIcon v-if="icon" :name="icon" data-slot="leadingIcon" :class="ui.leadingIcon({ class: uiProp?.leadingIcon })" />
<UAvatar v-else-if="avatar" :size="((uiProp?.leadingAvatarSize || ui.leadingAvatarSize()) as AvatarProps['size'])" v-bind="avatar" data-slot="leadingAvatar" :class="ui.leadingAvatar({ class: uiProp?.leadingAvatar })" />
</slot>
</div>
<div v-if="content || textParts.length || !!slots.content" data-slot="content" :class="ui.content({ class: uiProp?.content })">
<slot name="content" v-bind="{ ...messageProps, content }">
<template v-if="content">
{{ content }}
</template>
<template v-else>
<template v-for="(part, index) in textParts" :key="`${id}-${part.type}-${index}`">
{{ part.text }}
</template>
</template>
</slot>
</div>
<div v-if="actions || !!slots.actions" data-slot="actions" :class="ui.actions({ class: uiProp?.actions })">
<slot name="actions" v-bind="{ ...messageProps, actions }">
<UTooltip v-for="(action, index) in actions" :key="index" :text="action.label">
<UButton
size="sm"
color="neutral"
variant="ghost"
v-bind="omit(action, ['onClick'])"
:label="undefined"
@click="typeof action.onClick === 'function' ? action.onClick($event, messageProps) : undefined"
/>
</UTooltip>
</slot>
</div>
</div>
</Primitive>
</template>