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

@@ -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;
}