채팅 도구
채팅 도구 (ChatTool)
접을 수 있는 AI 도구 호출 상태를 표시하는 컴포넌트예요. "검색 중"이나 "문서 읽는 중" 같은 도구 실행 상태를 보여주고 접어서 도구 출력을 드러낼 수 있어요.
출처: Nuxt UI
본문
접을 수 있는 AI 도구 호출 상태를 표시해요.
사용 방법 (Usage)
ChatTool 컴포넌트는 "컴포넌트 검색 중" 또는 "문서 읽는 중" 같은 AI 도구 호출 상태를 표시하는 접을 수 있는 블록을 렌더링해요. 기본 슬롯이 제공되면 접을 수 있게 되어 도구 출력을 드러냅니다.
<script setup lang="ts">
const streaming = ref(true)
const result = ref(`$ pnpm run lint
> eslint .
✔ No lint errors found.
`)
let timer: ReturnType<typeof setTimeout> | undefined
onMounted(() => {
timer = setTimeout(() => {
streaming.value = false
}, 5000)
})
onUnmounted(() => {
clearTimeout(timer)
})
</script>
<template>
<UChatTool
:text="streaming ? 'Running lint checks' : 'Lint checks completed'"
suffix="cd, pnpm run"
:streaming="streaming"
icon="i-lucide-terminal"
variant="card"
chevron="leading"
class="w-80"
>
<pre language="bash" v-text="result" />
</UChatTool>
</template>
텍스트 (Text)
text prop을 사용하면 도구 상태 텍스트를 설정할 수 있어요.
<template>
<UChatTool text="Searched components" />
</template>
접미사 (Suffix)
suffix prop을 사용하면 메인 라벨 뒤에 보조 텍스트를 표시할 수 있어요.
<template>
<UChatTool text="Reading component" suffix="Button" />
</template>
스트리밍 (Streaming)
streaming prop을 사용하면 도구가 활발히 실행 중임을 표시할 수 있어요. 텍스트에 shimmer 애니메이션이 표시됩니다.
<template>
<UChatTool streaming text="Searching components..." />
</template>
[!TIP]
@nuxt/ui/utils/ai의isToolStreaming유틸리티를 사용하면 도구 파트가 아직 실행 중인지 판별할 수 있어요. 도구가 사용자 승인을 기다리는 동안에는false를 반환합니다.
Shimmer
스트리밍 중에는 트리거 라벨이 ChatShimmer 컴포넌트를 사용해요. shimmer prop으로 duration과 spread를 커스터마이즈할 수 있습니다.
<template>
<UChatTool streaming text="Searching components..." :shimmer="{
duration: 2,
spread: 2
}" />
</template>
아이콘 (Icon)
icon prop을 사용하면 트리거 옆에 Icon 컴포넌트를 표시할 수 있어요.
<template>
<UChatTool icon="i-lucide-search" text="Searched components" />
</template>
로딩 (Loading)
loading prop을 사용하면 로딩 표시기를 보여줄 수 있어요. loading-icon prop으로 로딩 아이콘을 커스터마이즈할 수 있습니다.
<template>
<UChatTool loading text="Searching components..." />
</template>
로딩 아이콘 (Loading Icon)
loading-icon prop을 사용하면 로딩 아이콘을 커스터마이즈할 수 있어요. 기본값은 i-lucide-loader-circle이에요.
<template>
<UChatTool loading loading-icon="i-lucide-loader" text="Searching components..." />
</template>
Nuxt:
[!TIP] See: /docs/getting-started/integrations/icons/nuxt#theme
app.config.ts의ui.icons.loading키에서 이 아이콘을 전역으로 커스터마이즈할 수 있어요.
Vue:
[!TIP] See: /docs/getting-started/integrations/icons/vue#theme
vite.config.ts의ui.icons.loading키에서 이 아이콘을 전역으로 커스터마이즈할 수 있어요.
셰브론 (Chevron)
chevron prop을 사용하면 셰브론 아이콘의 위치를 바꿀 수 있어요.
[!NOTE]
chevron을leading으로 설정하고icon이 있으면, 호버 시와 열림 상태에서 아이콘이 셰브론과 자리를 바꿉니다.
<template>
<UChatTool chevron="leading" icon="i-lucide-search" text="Searched components">
Tool output content
</UChatTool>
</template>
셰브론 아이콘 (Chevron Icon)
chevron-icon prop을 사용하면 셰브론 Icon을 커스터마이즈할 수 있어요. 기본값은 i-lucide-chevron-down이에요.
<template>
<UChatTool chevron-icon="i-lucide-arrow-down" text="Searched components">
Tool output content
</UChatTool>
</template>
Nuxt:
[!TIP] See: /docs/getting-started/integrations/icons/nuxt#theme
app.config.ts의ui.icons.chevronDown키에서 이 아이콘을 전역으로 커스터마이즈할 수 있어요.
Vue:
[!TIP] See: /docs/getting-started/integrations/icons/vue#theme
vite.config.ts의ui.icons.chevronDown키에서 이 아이콘을 전역으로 커스터마이즈할 수 있어요.
변형 (Variant)
variant prop을 사용하면 시각적 스타일을 바꿀 수 있어요. 기본값은 inline이에요.
<template>
<UChatTool variant="card" text="Searched components" icon="i-lucide-search" chevron="trailing">
Tool output content
</UChatTool>
</template>
액션 (Actions) 4.10+
actions prop을 사용하면 트리거 아래에 Button 목록을 표시할 수 있어요. 실행 전에 사용자 확인이 필요한 도구에 유용합니다.
<template>
<UChatTool :actions="[
{
label: 'Approve'
},
{
label: 'Deny',
color: 'neutral',
variant: 'soft'
}
]" text="Run terminal command" variant="card" icon="i-lucide-terminal">
$ pnpm run lint
</UChatTool>
</template>
예시 (Examples)
[!TIP] See: /docs/components/chat
설치 방법, 서버 설정, 사용 예시는 Chat 개요 페이지를 확인하세요.
승인 흐름 사용하기 (With approval flow) 4.10+
actions prop을 사용하면 AI SDK로 도구 승인 흐름을 만들 수 있어요. 도구 파트가 approval-requested 상태일 때 승인 및 거부 액션을 표시하고 addToolApprovalResponse로 응답합니다.
<script setup lang="ts">
import type { ButtonProps } from '@nuxt/ui'
const state = ref<'approval-requested' | 'output-available' | 'output-denied'>('approval-requested')
const result = ref('')
const text = computed(() => {
if (state.value === 'approval-requested') return 'Run terminal command'
if (state.value === 'output-denied') return 'Command cancelled'
return result.value ? 'Ran terminal command' : 'Running terminal command'
})
const output = computed(() => result.value || '$ pnpm run lint')
const actions = computed<ButtonProps[] | undefined>(() => {
if (state.value !== 'approval-requested') return undefined
return [
{ label: 'Approve', onClick: onApprove },
{ label: 'Deny', color: 'neutral', variant: 'soft', onClick: onDeny }
]
})
let timer: ReturnType<typeof setTimeout> | undefined
function onApprove() {
state.value = 'output-available'
timer = setTimeout(() => {
result.value = `$ pnpm run lint
> eslint .
✔ No lint errors found.
`
}, 2000)
}
function onDeny() {
state.value = 'output-denied'
}
function reset() {
clearTimeout(timer)
state.value = 'approval-requested'
result.value = ''
}
onUnmounted(() => {
clearTimeout(timer)
})
</script>
<template>
<div class="flex flex-col items-start gap-4">
<UChatTool
:text="text"
icon="i-lucide-terminal"
variant="card"
:streaming="state === 'output-available' && !result"
:actions="actions"
class="w-80"
>
<pre language="bash" v-text="output" />
</UChatTool>
<UButton
v-if="state !== 'approval-requested'"
label="Reset"
color="neutral"
variant="link"
size="xs"
icon="i-lucide-rotate-ccw"
class="p-0 absolute top-4 right-4"
@click="reset"
/>
</div>
</template>
[!TIP]
@nuxt/ui/utils/ai의isToolApprovalPending유틸리티를 사용하면 보류 중인 승인을 감지할 수 있어요. 이 상태에서는isToolStreaming이false를 반환합니다.<script setup lang="ts"> import { useChat } from '@ai-sdk/vue' import { lastAssistantMessageIsCompleteWithApprovalResponses } from 'ai' const { messages, addToolApprovalResponse } = useChat({ sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses }) </script> <template> <UChatTool v-if="isToolUIPart(part)" :text="getToolName(part)" :streaming="isToolStreaming(part)" :actions="part.state === 'approval-requested' ? [ { label: 'Approve', onClick: () => addToolApprovalResponse({ id: part.approval.id, approved: true }) }, { label: 'Deny', color: 'neutral', variant: 'ghost', onClick: () => addToolApprovalResponse({ id: part.approval.id, approved: false }) } ] : undefined" /> </template>
API
Props
/**
* Props for the ChatTool component
*/
interface ChatToolProps {
/**
* The text content to display.
*/
text?: string | undefined;
/**
* The suffix text displayed after the main text.
*/
suffix?: string | undefined;
/**
* The icon displayed next to the trigger.
*/
icon?: any;
/**
* Whether the tool is in a loading state.
* @default false
*/
loading?: boolean | undefined;
/**
* The icon displayed when loading.
* @default appConfig.ui.icons.loading
*/
loadingIcon?: any;
/**
* Whether the tool content is currently streaming.
* @default false
*/
streaming?: boolean | undefined;
/**
* The visual variant of the tool display.
* @default 'inline'
*/
variant?: "inline" | "card" | undefined;
/**
* The position of the chevron icon.
* @default 'trailing'
*/
chevron?: "trailing" | "leading" | undefined;
/**
* The icon displayed as the chevron.
* @default appConfig.ui.icons.chevronDown
*/
chevronIcon?: any;
/**
* Customize the [`ChatShimmer`](https://ui.nuxt.com/docs/components/chat-shimmer) component when streaming.
*/
shimmer?: Partial<Omit<ChatShimmerProps, "text">> | undefined;
/**
* Display a list of actions below the trigger, useful for tool approval flows.
* `{ size: 'xs' }`{lang="ts-type"}
*/
actions?: ButtonProps[] | undefined;
ui?: { root?: SlotClass; trigger?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; chevronIcon?: SlotClass; label?: SlotClass; suffix?: SlotClass; trailingIcon?: SlotClass; content?: SlotClass; body?: SlotClass; actions?: SlotClass; } | undefined;
/**
* When `true`, prevents the user from interacting with the collapsible.
*/
disabled?: boolean | undefined;
/**
* The controlled open state of the collapsible. Can be binded with `v-model`.
* @default undefined
*/
open?: boolean | undefined;
/**
* The open state of the collapsible when it is initially rendered. <br> Use when you do not need to control its open state.
*/
defaultOpen?: boolean | undefined;
/**
* When `true`, the element will be unmounted on closed state.
* @default false
*/
unmountOnHide?: boolean | undefined;
}
Slots
/**
* Slots for the ChatTool component
*/
interface ChatToolSlots {
default(): any;
actions(): any;
}
Emits
/**
* Emitted events for the ChatTool component
*/
interface ChatToolEmits {
update:open: (payload: [value: boolean]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
chatTool: {
slots: {
root: '',
trigger: [
'group flex w-full items-center gap-1.5 text-muted text-sm disabled:cursor-default disabled:hover:text-muted hover:text-default min-w-0',
'transition-colors'
],
leading: 'relative size-4 shrink-0',
leadingIcon: 'size-4 shrink-0',
chevronIcon: 'size-4 shrink-0 group-data-[state=open]:rotate-180 transition-transform duration-200 ease-out motion-reduce:transition-none',
label: 'truncate',
suffix: 'text-dimmed ms-1',
trailingIcon: 'size-4 shrink-0 group-data-[state=open]:rotate-180 transition-transform duration-200 ease-out motion-reduce:transition-none',
content: 'data-[state=open]:animate-[collapsible-down_200ms_var(--ease-out)] data-[state=closed]:animate-[collapsible-up_200ms_var(--ease-out)] data-[state=closed]:overflow-hidden',
body: 'text-sm text-dimmed whitespace-pre-wrap',
actions: 'flex items-center justify-end gap-1.5'
},
variants: {
variant: {
inline: {
trigger: 'rounded-sm outline-primary/25 focus-visible:outline-3',
body: 'pt-2',
actions: 'pt-2'
},
card: {
root: 'rounded-md ring ring-default overflow-hidden outline-primary/25 has-focus-visible:outline-3 has-focus-visible:ring-primary',
trigger: 'px-2 py-1 focus:outline-none',
trailingIcon: 'ms-auto',
body: 'border-t border-default p-2 max-h-[200px] overflow-y-auto focus:outline-none',
actions: 'border-t border-default p-2'
}
},
chevron: {
leading: '',
trailing: ''
},
loading: {
true: {
leadingIcon: 'animate-spin'
}
},
alone: {
false: {
leadingIcon: [
'absolute inset-0 group-hover:opacity-0 group-data-[state=open]:opacity-0',
'transition-opacity duration-200 ease-out'
],
chevronIcon: [
'absolute inset-0 opacity-0 group-hover:opacity-100 group-data-[state=open]:opacity-100',
'transition-[rotate,opacity] duration-200 ease-out motion-reduce:transition-none'
]
}
}
}
}
}
})