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,
|
||||
|
||||
@@ -68,29 +68,35 @@ public class AppDifyController {
|
||||
}
|
||||
|
||||
@GetMapping("/conversations")
|
||||
@Operation(summary = "获取会话列表")
|
||||
@Operation(summary = "获取会话列表(合并 pro + standard)")
|
||||
@Parameter(name = "agentId", description = "智能体ID", required = true)
|
||||
@Parameter(name = "lastId", description = "上一页最后一条记录ID")
|
||||
@Parameter(name = "source", description = "来源类型:agent-智能体 prompt-自建风格")
|
||||
@Parameter(name = "cursor", description = "复合游标(首页不传)")
|
||||
@Parameter(name = "limit", description = "返回条数,默认20")
|
||||
public CommonResult<DifyConversationListRespVO> getConversations(
|
||||
@RequestParam("agentId") Long agentId,
|
||||
@RequestParam(value = "lastId", required = false) String lastId,
|
||||
@RequestParam(value = "source", required = false, defaultValue = "agent") String source,
|
||||
@RequestParam(value = "cursor", required = false) String cursor,
|
||||
@RequestParam(value = "limit", required = false) Integer limit) {
|
||||
return CommonResult.success(difyService.getConversations(agentId, getCurrentUserId(), lastId, limit));
|
||||
return CommonResult.success(difyService.getConversations(agentId, source, getCurrentUserId(), cursor, limit));
|
||||
}
|
||||
|
||||
@GetMapping("/messages")
|
||||
@Operation(summary = "获取会话历史消息")
|
||||
@Operation(summary = "获取会话历史消息(自动定位 App)")
|
||||
@Parameter(name = "agentId", description = "智能体ID", required = true)
|
||||
@Parameter(name = "source", description = "来源类型:agent-智能体 prompt-自建风格")
|
||||
@Parameter(name = "conversationId", description = "会话ID", required = true)
|
||||
@Parameter(name = "appSource", description = "来源应用标识:pro/standard")
|
||||
@Parameter(name = "firstId", description = "当前页第一条记录ID")
|
||||
@Parameter(name = "limit", description = "返回条数,默认20")
|
||||
public CommonResult<DifyMessageListRespVO> getMessages(
|
||||
@RequestParam("agentId") Long agentId,
|
||||
@RequestParam(value = "source", required = false, defaultValue = "agent") String source,
|
||||
@RequestParam("conversationId") String conversationId,
|
||||
@RequestParam(value = "appSource", required = false) String appSource,
|
||||
@RequestParam(value = "firstId", required = false) String firstId,
|
||||
@RequestParam(value = "limit", required = false) Integer limit) {
|
||||
return CommonResult.success(difyService.getMessages(agentId, conversationId, getCurrentUserId(), firstId, limit));
|
||||
return CommonResult.success(difyService.getMessages(agentId, source, conversationId, appSource, getCurrentUserId(), firstId, limit));
|
||||
}
|
||||
|
||||
private String getCurrentUserId() {
|
||||
|
||||
@@ -53,26 +53,29 @@ public interface DifyService {
|
||||
Flux<DifyChatRespVO> promptAnalysisStream(PromptAnalysisReqVO reqVO, String userId);
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
* 获取会话列表(合并 pro + standard 两个 Dify App 的会话)
|
||||
*
|
||||
* @param agentId 智能体ID
|
||||
* @param source 来源类型(agent/prompt)
|
||||
* @param userId 用户ID
|
||||
* @param lastId 上一页最后一条记录ID
|
||||
* @param cursor 复合游标(Base64 编码,首页传 null)
|
||||
* @param limit 返回条数
|
||||
* @return 会话列表
|
||||
*/
|
||||
DifyConversationListRespVO getConversations(Long agentId, String userId, String lastId, Integer limit);
|
||||
DifyConversationListRespVO getConversations(Long agentId, String source, String userId, String cursor, Integer limit);
|
||||
|
||||
/**
|
||||
* 获取会话历史消息
|
||||
* 获取会话历史消息(自动定位 pro/standard App)
|
||||
*
|
||||
* @param agentId 智能体ID
|
||||
* @param source 来源类型(agent/prompt)
|
||||
* @param conversationId 会话ID
|
||||
* @param appSource 来源应用标识(pro/standard),用于选择 API Key
|
||||
* @param userId 用户ID
|
||||
* @param firstId 当前页第一条记录ID
|
||||
* @param limit 返回条数
|
||||
* @return 消息列表
|
||||
*/
|
||||
DifyMessageListRespVO getMessages(Long agentId, String conversationId, String userId, String firstId, Integer limit);
|
||||
DifyMessageListRespVO getMessages(Long agentId, String source, String conversationId, String appSource, String userId, String firstId, Integer limit);
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.tik.dify.vo.DifyChatReqVO;
|
||||
import cn.iocoder.yudao.module.tik.dify.vo.PromptAnalysisReqVO;
|
||||
import cn.iocoder.yudao.module.tik.dify.vo.DifyChatRespVO;
|
||||
import cn.iocoder.yudao.module.tik.dify.vo.DifyConversationListRespVO;
|
||||
import cn.iocoder.yudao.module.tik.dify.vo.DifyConversationRespVO;
|
||||
import cn.iocoder.yudao.module.tik.dify.vo.DifyMessageListRespVO;
|
||||
import cn.iocoder.yudao.module.tik.dify.vo.ForecastRewriteReqVO;
|
||||
import cn.iocoder.yudao.module.tik.enums.AiModelTypeEnum;
|
||||
@@ -21,7 +22,13 @@ import org.springframework.validation.annotation.Validated;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -50,9 +57,7 @@ public class DifyServiceImpl implements DifyService {
|
||||
AtomicLong pendingRecordId = new AtomicLong();
|
||||
AtomicReference<String> conversationIdRef = new AtomicReference<>(reqVO.getConversationId());
|
||||
AtomicReference<DifyChatRespVO> tokenUsageRef = new AtomicReference<>();
|
||||
String difyUserId = reqVO.getAgentId() != null
|
||||
? "user-" + userId + "-agent-" + reqVO.getAgentId()
|
||||
: "user-" + userId + "-prompt";
|
||||
String difyUserId = buildDifyUserId(userId, reqVO.getSource(), reqVO.getAgentId());
|
||||
String logPrefix = "chatStream";
|
||||
|
||||
return Mono.fromCallable(() -> {
|
||||
@@ -380,55 +385,169 @@ public class DifyServiceImpl implements DifyService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DifyConversationListRespVO getConversations(Long agentId, String userId, String lastId, Integer limit) {
|
||||
// 获取智能体配置
|
||||
AiAgentDO agent = aiAgentService.getAiAgent(agentId);
|
||||
if (agent == null) {
|
||||
throw new RuntimeException("智能体不存在");
|
||||
public DifyConversationListRespVO getConversations(Long agentId, String source, String userId, String cursor, Integer limit) {
|
||||
if (limit == null || limit <= 0) {
|
||||
limit = 20;
|
||||
}
|
||||
|
||||
// 获取积分配置(使用标准模式的 API Key)
|
||||
AiServiceConfigDO config = pointsService.getConfig(
|
||||
AiPlatformEnum.DIFY.getPlatform(),
|
||||
AiModelTypeEnum.DIFY_WRITING_STANDARD.getModelCode());
|
||||
AiServiceConfigDO proConfig = getDifyConfig(false);
|
||||
AiServiceConfigDO standardConfig = getDifyConfig(true);
|
||||
|
||||
// Dify 用户标识(按 agentId 隔离会话)
|
||||
String difyUserId = "user-" + userId + "-agent-" + agentId;
|
||||
String difyUserId = buildDifyUserId(userId, source, agentId);
|
||||
|
||||
DifyConversationListRespVO result = difyClient.getConversations(config.getApiKey(), difyUserId, lastId, limit);
|
||||
String proLastId = null;
|
||||
String standardLastId = null;
|
||||
if (cursor != null && !cursor.isEmpty()) {
|
||||
try {
|
||||
String json = new String(Base64.getDecoder().decode(cursor));
|
||||
com.fasterxml.jackson.databind.JsonNode node = JsonUtils.parseTree(json);
|
||||
proLastId = node.has("pro") ? node.get("pro").asText(null) : null;
|
||||
standardLastId = node.has("standard") ? node.get("standard").asText(null) : null;
|
||||
} catch (Exception e) {
|
||||
log.warn("[getConversations] 游标解析失败,从头开始: {}", cursor, e);
|
||||
}
|
||||
}
|
||||
|
||||
List<DifyConversationRespVO> proList = Collections.emptyList();
|
||||
List<DifyConversationRespVO> standardList = Collections.emptyList();
|
||||
boolean proHasMore = false;
|
||||
boolean standardHasMore = false;
|
||||
|
||||
try {
|
||||
DifyConversationListRespVO proResult = difyClient.getConversations(
|
||||
proConfig.getApiKey(), difyUserId, proLastId, limit);
|
||||
if (proResult != null && proResult.getData() != null) {
|
||||
proList = proResult.getData();
|
||||
proList.forEach(c -> c.setAppSource("pro"));
|
||||
proHasMore = proResult.getHasMore() != null && proResult.getHasMore();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[getConversations] 查询 Pro 会话列表失败", e);
|
||||
}
|
||||
|
||||
try {
|
||||
DifyConversationListRespVO standardResult = difyClient.getConversations(
|
||||
standardConfig.getApiKey(), difyUserId, standardLastId, limit);
|
||||
if (standardResult != null && standardResult.getData() != null) {
|
||||
standardList = standardResult.getData();
|
||||
standardList.forEach(c -> c.setAppSource("standard"));
|
||||
standardHasMore = standardResult.getHasMore() != null && standardResult.getHasMore();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[getConversations] 查询 Standard 会话列表失败", e);
|
||||
}
|
||||
|
||||
// 合并两个列表,按 updatedAt 降序排序,截取 limit 条
|
||||
List<DifyConversationRespVO> merged = new ArrayList<>();
|
||||
merged.addAll(proList);
|
||||
merged.addAll(standardList);
|
||||
merged.sort((a, b) -> {
|
||||
long timeA = a.getUpdatedAt() != null ? a.getUpdatedAt() : (a.getCreatedAt() != null ? a.getCreatedAt() : 0);
|
||||
long timeB = b.getUpdatedAt() != null ? b.getUpdatedAt() : (b.getCreatedAt() != null ? b.getCreatedAt() : 0);
|
||||
return Long.compare(timeB, timeA); // 降序
|
||||
});
|
||||
|
||||
// 判断是否有更多数据
|
||||
boolean hasMore = proHasMore || standardHasMore;
|
||||
|
||||
List<DifyConversationRespVO> pageData;
|
||||
if (merged.size() > limit) {
|
||||
pageData = new ArrayList<>(merged.subList(0, limit));
|
||||
hasMore = true;
|
||||
} else {
|
||||
pageData = merged;
|
||||
}
|
||||
|
||||
// 过滤掉 inputs 中的敏感字段(如 sysPrompt)
|
||||
if (result != null && result.getData() != null) {
|
||||
result.getData().forEach(conv -> {
|
||||
if (conv.getInputs() != null) {
|
||||
conv.getInputs().remove("sysPrompt");
|
||||
}
|
||||
});
|
||||
}
|
||||
pageData.forEach(conv -> {
|
||||
if (conv.getInputs() != null) {
|
||||
conv.getInputs().remove("sysPrompt");
|
||||
}
|
||||
});
|
||||
|
||||
// 构建下一页游标:收集当前页中 pro 和 standard 的最后一条记录ID
|
||||
String nextCursor = buildNextCursor(pageData, proHasMore, standardHasMore);
|
||||
|
||||
DifyConversationListRespVO result = new DifyConversationListRespVO();
|
||||
result.setLimit(limit);
|
||||
result.setHasMore(hasMore);
|
||||
result.setData(pageData);
|
||||
result.setNextCursor(nextCursor);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DifyMessageListRespVO getMessages(Long agentId, String conversationId, String userId, String firstId, Integer limit) {
|
||||
// 获取智能体配置
|
||||
AiAgentDO agent = aiAgentService.getAiAgent(agentId);
|
||||
if (agent == null) {
|
||||
throw new RuntimeException("智能体不存在");
|
||||
/**
|
||||
* 构建下一页复合游标
|
||||
*/
|
||||
private String buildNextCursor(List<DifyConversationRespVO> pageData,
|
||||
boolean proHasMore, boolean standardHasMore) {
|
||||
if (pageData.isEmpty()) return null;
|
||||
|
||||
String proLastId = null;
|
||||
String standardLastId = null;
|
||||
|
||||
for (int i = pageData.size() - 1; i >= 0; i--) {
|
||||
DifyConversationRespVO conv = pageData.get(i);
|
||||
if ("pro".equals(conv.getAppSource()) && proLastId == null && proHasMore) {
|
||||
proLastId = conv.getId();
|
||||
}
|
||||
if ("standard".equals(conv.getAppSource()) && standardLastId == null && standardHasMore) {
|
||||
standardLastId = conv.getId();
|
||||
}
|
||||
if (proLastId != null && standardLastId != null) break;
|
||||
}
|
||||
|
||||
// 获取积分配置(使用标准模式的 API Key)
|
||||
AiServiceConfigDO config = pointsService.getConfig(
|
||||
AiPlatformEnum.DIFY.getPlatform(),
|
||||
AiModelTypeEnum.DIFY_WRITING_STANDARD.getModelCode());
|
||||
if (proLastId == null && standardLastId == null) return null;
|
||||
|
||||
// Dify 用户标识(按 agentId 隔离会话)
|
||||
String difyUserId = "user-" + userId + "-agent-" + agentId;
|
||||
try {
|
||||
Map<String, String> cursorMap = new HashMap<>();
|
||||
if (proLastId != null) cursorMap.put("pro", proLastId);
|
||||
if (standardLastId != null) cursorMap.put("standard", standardLastId);
|
||||
String json = JsonUtils.toJsonString(cursorMap);
|
||||
return Base64.getEncoder().encodeToString(json.getBytes());
|
||||
} catch (Exception e) {
|
||||
log.warn("[buildNextCursor] 构建游标失败", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
DifyMessageListRespVO result = difyClient.getMessages(config.getApiKey(), conversationId, difyUserId, firstId, limit);
|
||||
@Override
|
||||
public DifyMessageListRespVO getMessages(Long agentId, String source, String conversationId,
|
||||
String appSource, String userId, String firstId, Integer limit) {
|
||||
String difyUserId = buildDifyUserId(userId, source, agentId);
|
||||
|
||||
boolean isStandard = "standard".equals(appSource);
|
||||
AiServiceConfigDO primaryConfig = getDifyConfig(isStandard);
|
||||
AiServiceConfigDO fallbackConfig = null;
|
||||
|
||||
DifyMessageListRespVO result = null;
|
||||
try {
|
||||
result = difyClient.getMessages(primaryConfig.getApiKey(), conversationId, difyUserId, firstId, limit);
|
||||
} catch (Exception e) {
|
||||
log.warn("[getMessages] 主 Key 查询失败,appSource: {}, 尝试降级", appSource, e);
|
||||
}
|
||||
|
||||
if (result == null || result.getData() == null || result.getData().isEmpty()) {
|
||||
fallbackConfig = getDifyConfig(!isStandard);
|
||||
try {
|
||||
DifyMessageListRespVO fallbackResult = difyClient.getMessages(
|
||||
fallbackConfig.getApiKey(), conversationId, difyUserId, firstId, limit);
|
||||
if (fallbackResult != null && fallbackResult.getData() != null && !fallbackResult.getData().isEmpty()) {
|
||||
result = fallbackResult;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[getMessages] 降级 Key 查询也失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null) {
|
||||
result = new DifyMessageListRespVO();
|
||||
result.setData(Collections.emptyList());
|
||||
result.setHasMore(false);
|
||||
}
|
||||
|
||||
// 过滤掉 inputs 中的敏感字段(如 sysPrompt)
|
||||
if (result != null && result.getData() != null) {
|
||||
if (result.getData() != null) {
|
||||
result.getData().forEach(msg -> {
|
||||
if (msg.getInputs() != null) {
|
||||
msg.getInputs().remove("sysPrompt");
|
||||
@@ -439,4 +558,18 @@ public class DifyServiceImpl implements DifyService {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String resolveScope(String source) {
|
||||
return "prompt".equals(source) ? "prompt" : "agent";
|
||||
}
|
||||
|
||||
private String buildDifyUserId(String userId, String source, Long agentId) {
|
||||
return "user-" + userId + "-" + resolveScope(source) + "-" + agentId;
|
||||
}
|
||||
|
||||
private AiServiceConfigDO getDifyConfig(boolean standard) {
|
||||
return pointsService.getConfig(
|
||||
AiPlatformEnum.DIFY.getPlatform(),
|
||||
(standard ? AiModelTypeEnum.DIFY_WRITING_STANDARD : AiModelTypeEnum.DIFY_WRITING_PRO).getModelCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,4 +28,7 @@ public class DifyChatReqVO {
|
||||
@Schema(description = "自定义系统提示词(使用用户自建风格时传入)")
|
||||
private String customSystemPrompt;
|
||||
|
||||
@Schema(description = "来源类型:agent-智能体 prompt-自建风格", example = "agent")
|
||||
private String source;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.iocoder.yudao.module.tik.dify.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -23,4 +24,8 @@ public class DifyConversationListRespVO {
|
||||
@Schema(description = "会话列表", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private List<DifyConversationRespVO> data;
|
||||
|
||||
@Schema(description = "下一页游标(Base64 编码的复合游标)")
|
||||
@JsonProperty("next_cursor")
|
||||
private String nextCursor;
|
||||
|
||||
}
|
||||
|
||||
@@ -38,4 +38,8 @@ public class DifyConversationRespVO {
|
||||
@JsonProperty("updated_at")
|
||||
private Long updatedAt;
|
||||
|
||||
@Schema(description = "来源应用标识:pro/standard", example = "pro")
|
||||
@JsonProperty("app_source")
|
||||
private String appSource;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user