useToolCall 提供 AI 聊天功能中可用的工具定义。当 AI 助手调用工具时,会根据此配置显示相应的标签信息,提升用户体验。
工具定义支持两种形式:
<script setup lang="ts">
const { tools } = useToolCall()
function getToolLabel(toolName: string, args?: any): string {
const label = tools[toolName]
if (!label) {
return toolName
}
return typeof label === 'function' ? label(args) : label
}
// 示例:获取工具标签
console.log(getToolLabel('list-pages'))
// 输出:列出所有文档页面
console.log(getToolLabel('get-page', { path: '/docs/api' }))
// 输出:检索 /docs/api
</script>
如果需要自定义工具定义,可以在应用层覆盖此 composable:
export function useToolCall() {
const tools: Record<string, string | ((args: any) => string)> = {
// 静态工具标签
'list-pages': '列出所有文档页面',
'search-docs': '搜索文档内容',
// 动态工具标签
'get-page': (args: any) => `检索 ${args?.path || '页面'}`,
'update-content': (args: any) => {
const action = args?.action || '更新'
const target = args?.target || '内容'
return `${action}${target}`
}
}
return {
tools
}
}
useToolCall()返回工具定义对象,包含所有可用的 AI 工具及其显示标签。
2c361 — refactor: 重构 useTools 为 useToolCall 并简化 API