feat(agent): 支持自建风格与智能体双模式对话系统
Some checks failed
Build and Deploy / deploy (push) Has been cancelled
Some checks failed
Build and Deploy / deploy (push) Has been cancelled
- 新增 `source` 字段区分智能体(`agent`)与自建风格(`prompt`)两种对话来源 - 前端统一对话组件,根据来源动态构建请求参数、显示不同样式与文案 - 后端重构 Dify 会话与消息获取逻辑,支持合并查询 Pro 与 Standard 两个 Dify App 的会话历史 - 实现复合游标分页机制,支持跨双数据源的高效分页 - 新增 `clipboard-polyfill` 依赖,统一剪贴板复制功能,提升非 HTTPS 环境兼容性 - 扩展历史记录面板,支持按来源加载对应会话与消息 - 调整侧边抽屉宽度,优化大屏显示体验
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
"ai": "^6.0.39",
|
||||
"aplayer": "^1.10.1",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clipboard-polyfill": "^4.1.1",
|
||||
"clsx": "^2.1.1",
|
||||
"date-fns": "^4.1.0",
|
||||
"dayjs": "^1.11.18",
|
||||
|
||||
@@ -38,6 +38,7 @@ export async function sendChatStream(options) {
|
||||
conversationId,
|
||||
modelMode = 'pro',
|
||||
customSystemPrompt,
|
||||
source,
|
||||
ctrl,
|
||||
onMessage,
|
||||
onError,
|
||||
@@ -59,7 +60,8 @@ export async function sendChatStream(options) {
|
||||
content,
|
||||
conversationId,
|
||||
modelMode,
|
||||
customSystemPrompt
|
||||
customSystemPrompt,
|
||||
source
|
||||
}),
|
||||
onmessage: (event) => {
|
||||
if (typeof onMessage === 'function') {
|
||||
@@ -89,10 +91,11 @@ export async function sendChatStream(options) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
* 获取会话列表(合并 pro + standard 两个 Dify 工作流)
|
||||
* @param {Object} params - 请求参数
|
||||
* @param {number} params.agentId - 智能体ID
|
||||
* @param {string} [params.lastId] - 上一页最后一条记录ID
|
||||
* @param {string} [params.source] - 来源类型:agent-智能体 prompt-自建风格
|
||||
* @param {string} [params.cursor] - 复合游标(首页不传)
|
||||
* @param {number} [params.limit] - 返回条数,默认20
|
||||
*/
|
||||
export function getConversations(params) {
|
||||
@@ -104,10 +107,12 @@ export function getConversations(params) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话历史消息
|
||||
* 获取会话历史消息(自动定位 pro/standard App)
|
||||
* @param {Object} params - 请求参数
|
||||
* @param {number} params.agentId - 智能体ID
|
||||
* @param {string} [params.source] - 来源类型:agent-智能体 prompt-自建风格
|
||||
* @param {string} params.conversationId - 会话ID
|
||||
* @param {string} [params.appSource] - 来源应用标识:pro/standard
|
||||
* @param {string} [params.firstId] - 当前页第一条记录ID
|
||||
* @param {number} [params.limit] - 返回条数,默认20
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup>
|
||||
import { ref, watch, nextTick } from 'vue'
|
||||
import { ref, computed, watch, nextTick } from 'vue'
|
||||
import { Icon } from '@iconify/vue'
|
||||
import { toast } from 'vue-sonner'
|
||||
import {
|
||||
@@ -33,7 +33,46 @@ const props = defineProps({
|
||||
|
||||
const emit = defineEmits(['update:visible', 'send'])
|
||||
|
||||
// State
|
||||
const isPromptScene = computed(() => props.agent?.source === 'prompt')
|
||||
|
||||
function buildRequestOptions(prompt, ctrl) {
|
||||
const base = {
|
||||
agentId: props.agent?.id,
|
||||
source: props.agent?.source || 'agent',
|
||||
content: prompt,
|
||||
modelMode: modelMode.value,
|
||||
ctrl,
|
||||
onMessage: handleStreamMessage,
|
||||
onError: handleStreamError,
|
||||
onClose: handleStreamClose
|
||||
}
|
||||
|
||||
if (isPromptScene.value) {
|
||||
base.customSystemPrompt = props.agent.customSystemPrompt
|
||||
}
|
||||
|
||||
return base
|
||||
}
|
||||
|
||||
function handleStreamMessage(result) {
|
||||
if (result.event === 'message' && result.content) {
|
||||
generatedContent.value += result.content
|
||||
scrollToBottom()
|
||||
} else if (result.event === 'error') {
|
||||
toast.error(result.errorMessage || '生成出错')
|
||||
}
|
||||
}
|
||||
|
||||
function handleStreamError() {
|
||||
toast.error('生成失败')
|
||||
if (!generatedContent.value) isGenerating.value = false
|
||||
}
|
||||
|
||||
function handleStreamClose() {
|
||||
isGenerating.value = false
|
||||
abortController.value = null
|
||||
}
|
||||
|
||||
const modelMode = ref('pro')
|
||||
const inputText = ref('')
|
||||
const isGenerating = ref(false)
|
||||
@@ -44,7 +83,6 @@ const abortController = ref(null)
|
||||
const historyVisible = ref(false)
|
||||
const showCloseConfirm = ref(false)
|
||||
|
||||
// Methods
|
||||
const handleClose = (open) => {
|
||||
if (!open) {
|
||||
if (isGenerating.value) {
|
||||
@@ -122,29 +160,7 @@ const executeStreamRequest = async (prompt) => {
|
||||
abortController.value = new AbortController()
|
||||
|
||||
try {
|
||||
await sendChatStream({
|
||||
agentId: props.agent?.customSystemPrompt ? undefined : props.agent?.id,
|
||||
customSystemPrompt: props.agent?.customSystemPrompt,
|
||||
content: prompt,
|
||||
modelMode: modelMode.value,
|
||||
ctrl: abortController.value,
|
||||
onMessage: (result) => {
|
||||
if (result.event === 'message' && result.content) {
|
||||
generatedContent.value += result.content
|
||||
scrollToBottom()
|
||||
} else if (result.event === 'error') {
|
||||
toast.error(result.errorMessage || '生成出错')
|
||||
}
|
||||
},
|
||||
onError: () => {
|
||||
toast.error('生成失败')
|
||||
if (!generatedContent.value) isGenerating.value = false
|
||||
},
|
||||
onClose: () => {
|
||||
isGenerating.value = false
|
||||
abortController.value = null
|
||||
}
|
||||
})
|
||||
await sendChatStream(buildRequestOptions(prompt, abortController.value))
|
||||
} catch (error) {
|
||||
if (error.name !== 'AbortError') toast.error('生成失败')
|
||||
isGenerating.value = false
|
||||
@@ -209,6 +225,7 @@ watch(() => props.visible, (val) => {
|
||||
<HistoryPanel
|
||||
:visible="historyVisible"
|
||||
:agent-id="agent?.id"
|
||||
:source="agent?.source || 'agent'"
|
||||
@close="closeHistory"
|
||||
/>
|
||||
|
||||
|
||||
@@ -1,31 +1,45 @@
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
import { Icon } from '@iconify/vue'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
||||
defineProps({
|
||||
const props = defineProps({
|
||||
agent: { type: Object, default: null }
|
||||
})
|
||||
|
||||
const emit = defineEmits(['history'])
|
||||
|
||||
const isPromptScene = computed(() => props.agent?.source === 'prompt')
|
||||
|
||||
const openHistory = () => emit('history')
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="flex items-center justify-between px-4 py-3 border-b border-border/50 shrink-0 bg-gradient-to-r from-background to-muted/30">
|
||||
<div class="flex items-center gap-3">
|
||||
<div class="size-10 rounded-xl bg-gradient-to-br from-primary to-violet-500 flex items-center justify-center overflow-hidden shadow-sm shadow-primary/20">
|
||||
<div
|
||||
class="size-10 rounded-xl flex items-center justify-center overflow-hidden shadow-sm"
|
||||
:class="isPromptScene
|
||||
? 'bg-gradient-to-br from-amber-400 to-orange-500 shadow-amber-200/30'
|
||||
: 'bg-gradient-to-br from-primary to-violet-500 shadow-primary/20'"
|
||||
>
|
||||
<img
|
||||
v-if="agent?.avatar"
|
||||
:src="agent.avatar"
|
||||
:alt="agent.name"
|
||||
class="w-full h-full object-cover"
|
||||
/>
|
||||
<Icon v-else icon="lucide:bot" class="text-white text-xl" />
|
||||
<Icon v-else :icon="isPromptScene ? 'lucide:sparkles' : 'lucide:bot'" class="text-white text-xl" />
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-semibold text-foreground text-sm">{{ agent?.name || 'AI 助手' }}</div>
|
||||
<div class="text-xs text-muted-foreground">{{ agent?.categoryName || '通用' }}</div>
|
||||
<div class="text-xs text-muted-foreground">
|
||||
<span v-if="isPromptScene" class="inline-flex items-center gap-1">
|
||||
<Icon icon="lucide:pencil" class="size-3" />
|
||||
{{ agent?.categoryName || '我的风格' }}
|
||||
</span>
|
||||
<span v-else>{{ agent?.categoryName || '通用' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
|
||||
@@ -172,7 +172,8 @@ import { copyToClipboard } from '@/utils/clipboard'
|
||||
|
||||
const props = defineProps({
|
||||
visible: { type: Boolean, default: false },
|
||||
agentId: { type: [String, Number], default: null }
|
||||
agentId: { type: [String, Number], default: null },
|
||||
source: { type: String, default: 'agent' }
|
||||
})
|
||||
|
||||
const emit = defineEmits(['close'])
|
||||
@@ -183,7 +184,7 @@ const conversationList = ref([])
|
||||
const selectedConversation = ref(null)
|
||||
const messageList = ref([])
|
||||
const messageLoading = ref(false)
|
||||
const lastId = ref(null)
|
||||
const cursor = ref(null)
|
||||
const hasMore = ref(true)
|
||||
|
||||
// 按日期分组(先排序再分组)
|
||||
@@ -229,9 +230,9 @@ const loadConversations = async (loadMore = false) => {
|
||||
|
||||
loading.value = true
|
||||
try {
|
||||
const params = { agentId: props.agentId, limit: 20 }
|
||||
if (loadMore && lastId.value) {
|
||||
params.lastId = lastId.value
|
||||
const params = { agentId: props.agentId, source: props.source, limit: 20 }
|
||||
if (loadMore && cursor.value) {
|
||||
params.cursor = cursor.value
|
||||
}
|
||||
const res = await getConversations(params)
|
||||
if (res.code === 0) {
|
||||
@@ -241,9 +242,9 @@ const loadConversations = async (loadMore = false) => {
|
||||
} else {
|
||||
conversationList.value = data
|
||||
}
|
||||
// 更新分页状态
|
||||
lastId.value = data.length > 0 ? data[data.length - 1].id : null
|
||||
hasMore.value = data.length === 20 && res.data?.hasMore !== false
|
||||
// 更新分页状态:使用后端返回的复合游标
|
||||
cursor.value = res.data?.nextCursor || null
|
||||
hasMore.value = res.data?.hasMore !== false && data.length > 0
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('加载会话列表失败:', e)
|
||||
@@ -259,7 +260,9 @@ const selectConversation = async (conversation) => {
|
||||
try {
|
||||
const res = await getMessages({
|
||||
agentId: props.agentId,
|
||||
source: props.source,
|
||||
conversationId: conversation.id,
|
||||
appSource: conversation.appSource,
|
||||
limit: 50
|
||||
})
|
||||
if (res.code === 0) {
|
||||
|
||||
@@ -94,6 +94,7 @@ async function handleDelete(id) {
|
||||
|
||||
function handleUse(item) {
|
||||
emit('chat', {
|
||||
source: 'prompt',
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
categoryName: item.category || '我的风格',
|
||||
|
||||
@@ -39,9 +39,9 @@ const forwarded = useForwardPropsEmits(delegatedProps, emits)
|
||||
:class="cn(
|
||||
'bg-background data-[state=open]:animate-in data-[state=closed]:animate-out fixed z-50 flex flex-col gap-4 shadow-lg transition ease-in-out data-[state=closed]:duration-300 data-[state=open]:duration-500',
|
||||
side === 'right'
|
||||
&& 'data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-sm',
|
||||
&& 'data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-2xl',
|
||||
side === 'left'
|
||||
&& 'data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left inset-y-0 left-0 h-full w-3/4 border-r sm:max-w-sm',
|
||||
&& 'data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left inset-y-0 left-0 h-full w-3/4 border-r sm:max-w-2xl',
|
||||
side === 'top'
|
||||
&& 'data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top inset-x-0 top-0 h-auto border-b',
|
||||
side === 'bottom'
|
||||
|
||||
@@ -1,41 +1,17 @@
|
||||
import * as clipboardPolyfill from 'clipboard-polyfill'
|
||||
|
||||
/**
|
||||
* 复制文本到剪贴板
|
||||
* 兼容非 HTTPS 环境的降级方案
|
||||
* 使用 clipboard-polyfill 兼容非 HTTPS 环境
|
||||
*/
|
||||
export async function copyToClipboard(text: string): Promise<boolean> {
|
||||
if (!text?.trim()) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 优先使用 Clipboard API(需要 HTTPS 或 localhost)
|
||||
if (navigator.clipboard?.writeText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text)
|
||||
return true
|
||||
} catch {
|
||||
// 降级到 execCommand 方案
|
||||
}
|
||||
}
|
||||
|
||||
// 降级方案:使用 textarea + execCommand
|
||||
return fallbackCopy(text)
|
||||
}
|
||||
|
||||
/**
|
||||
* 降级复制方案
|
||||
*/
|
||||
function fallbackCopy(text: string): boolean {
|
||||
try {
|
||||
const textarea = document.createElement('textarea')
|
||||
textarea.value = text
|
||||
textarea.style.position = 'fixed'
|
||||
textarea.style.opacity = '0'
|
||||
textarea.style.left = '-9999px'
|
||||
document.body.appendChild(textarea)
|
||||
textarea.select()
|
||||
const success = document.execCommand('copy')
|
||||
document.body.removeChild(textarea)
|
||||
return success
|
||||
await clipboardPolyfill.writeText(text)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -280,6 +280,7 @@ const fetchAgentList = async () => {
|
||||
const res = await getAgentList()
|
||||
if (res.code === 0 && res.data) {
|
||||
agentList.value = res.data.map(item => ({
|
||||
source: 'agent',
|
||||
id: item.id,
|
||||
agentId: item.agentId,
|
||||
name: item.agentName,
|
||||
|
||||
Reference in New Issue
Block a user