feat(agent): 支持自定义系统提示词进行对话
Some checks failed
Build and Deploy / deploy (push) Has been cancelled

- 前端 API 新增 customSystemPrompt 参数,agentId 变为可选
- 聊天抽屉支持自定义提示词时传递参数并调整宽度
- 我的收藏模态框返回提示词内容供聊天使用
- 后端 Dify 服务优先使用自定义提示词,支持无 agentId 的对话
- Dify 请求 VO 中 agentId 改为非必填,新增 customSystemPrompt 字段
This commit is contained in:
2026-04-11 16:22:11 +08:00
parent e169065653
commit 09a567a542
5 changed files with 35 additions and 13 deletions

View File

@@ -21,10 +21,11 @@ export function getAgentList() {
/**
* 流式对话SSE
* @param {Object} options - 请求配置
* @param {number} options.agentId - 智能体ID
* @param {number} [options.agentId] - 智能体ID(使用 customSystemPrompt 时可不传)
* @param {string} options.content - 用户输入内容
* @param {string} [options.conversationId] - 会话ID可选首次对话不传
* @param {string} [options.modelMode] - 模型模式pro-深度版 standard-标准版
* @param {string} [options.customSystemPrompt] - 自定义系统提示词(使用用户自建风格时传入)
* @param {AbortController} [options.ctrl] - 取消控制器
* @param {Function} options.onMessage - 消息回调
* @param {Function} [options.onError] - 错误回调
@@ -36,6 +37,7 @@ export async function sendChatStream(options) {
content,
conversationId,
modelMode = 'pro',
customSystemPrompt,
ctrl,
onMessage,
onError,
@@ -56,7 +58,8 @@ export async function sendChatStream(options) {
agentId,
content,
conversationId,
modelMode
modelMode,
customSystemPrompt
}),
onmessage: (event) => {
if (typeof onMessage === 'function') {

View File

@@ -123,7 +123,8 @@ const executeStreamRequest = async (prompt) => {
try {
await sendChatStream({
agentId: props.agent?.id,
agentId: props.agent?.customSystemPrompt ? undefined : props.agent?.id,
customSystemPrompt: props.agent?.customSystemPrompt,
content: prompt,
modelMode: modelMode.value,
ctrl: abortController.value,
@@ -170,7 +171,7 @@ watch(() => props.visible, (val) => {
<template>
<Sheet :open="visible" @update:open="handleClose">
<SheetContent side="right" class="w-full sm:w-[420px] md:w-[560px] lg:w-[640px] p-0 flex flex-col bg-background">
<SheetContent side="right" class="w-full sm:w-[520px] md:w-[680px] lg:w-[800px] p-0 flex flex-col bg-background">
<!-- Header -->
<SheetHeader class="shrink-0">
<ChatDrawerHeader :agent="agent" @history="openHistory" />

View File

@@ -60,6 +60,7 @@ async function loadList() {
promptList.value = res.data.map(item => ({
id: item.id,
name: item.name,
content: item.content,
category: item.category,
useCount: item.useCount || 0
}))
@@ -95,7 +96,8 @@ function handleUse(item) {
emit('chat', {
id: item.id,
name: item.name,
categoryName: item.category || '我的风格'
categoryName: item.category || '我的风格',
customSystemPrompt: item.content
})
handleClose()
}

View File

@@ -50,13 +50,24 @@ public class DifyServiceImpl implements DifyService {
AtomicLong pendingRecordId = new AtomicLong();
AtomicReference<String> conversationIdRef = new AtomicReference<>(reqVO.getConversationId());
AtomicReference<DifyChatRespVO> tokenUsageRef = new AtomicReference<>();
String difyUserId = "user-" + userId + "-agent-" + reqVO.getAgentId();
String difyUserId = reqVO.getAgentId() != null
? "user-" + userId + "-agent-" + reqVO.getAgentId()
: "user-" + userId + "-prompt";
String logPrefix = "chatStream";
return Mono.fromCallable(() -> {
AiAgentDO agent = aiAgentService.getAiAgent(reqVO.getAgentId());
if (agent == null) {
throw new RuntimeException("智能体不存在");
// 解析系统提示词:优先使用自定义提示词,否则从智能体获取
String systemPrompt;
if (reqVO.getCustomSystemPrompt() != null && !reqVO.getCustomSystemPrompt().isEmpty()) {
systemPrompt = reqVO.getCustomSystemPrompt();
} else if (reqVO.getAgentId() != null) {
AiAgentDO agent = aiAgentService.getAiAgent(reqVO.getAgentId());
if (agent == null) {
throw new RuntimeException("智能体不存在");
}
systemPrompt = agent.getSystemPrompt();
} else {
throw new RuntimeException("必须提供 agentId 或 customSystemPrompt");
}
AiModelTypeEnum modelTypeEnum = "standard".equals(reqVO.getModelMode())
@@ -66,12 +77,15 @@ public class DifyServiceImpl implements DifyService {
AiPlatformEnum.DIFY.getPlatform(), modelTypeEnum.getModelCode());
pointsService.checkPoints(userId, config.getConsumePoints());
String serviceRef = reqVO.getAgentId() != null
? reqVO.getAgentId().toString()
: "custom-prompt";
Long recordId = pointsService.createPendingDeduct(
userId, config.getConsumePoints(), "dify_chat",
reqVO.getAgentId().toString(), config.getServiceCode());
serviceRef, config.getServiceCode());
pendingRecordId.set(recordId);
return new DifyChatContext(agent.getSystemPrompt(), config.getApiKey(), config.getConsumePoints());
return new DifyChatContext(systemPrompt, config.getApiKey(), config.getConsumePoints());
})
.flatMapMany(context -> difyClient.chatStream(
context.apiKey(), reqVO.getContent(), context.systemPrompt(),

View File

@@ -12,8 +12,7 @@ import lombok.Data;
@Data
public class DifyChatReqVO {
@Schema(description = "智能体ID", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "智能体ID不能为空")
@Schema(description = "智能体ID(使用 customSystemPrompt 时可不传)")
private Long agentId;
@Schema(description = "用户输入内容", requiredMode = Schema.RequiredMode.REQUIRED)
@@ -26,4 +25,7 @@ public class DifyChatReqVO {
@Schema(description = "模型模式pro-深度版 standard-标准版", example = "pro")
private String modelMode = "pro";
@Schema(description = "自定义系统提示词(使用用户自建风格时传入)")
private String customSystemPrompt;
}