对话、重新生成、评价完成
This commit is contained in:
@@ -81,6 +81,8 @@ CREATE TABLE `tb_ai_conversation` (
|
||||
`last_message_time` TIMESTAMP NULL DEFAULT NULL COMMENT '最后消息时间',
|
||||
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
`delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
|
||||
`deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_user_createtime` (`user_id`, `create_time` DESC),
|
||||
KEY `idx_user_favorite` (`user_id`, `is_favorite`),
|
||||
@@ -96,6 +98,7 @@ CREATE TABLE `tb_ai_message` (
|
||||
`id` VARCHAR(50) NOT NULL COMMENT '消息ID',
|
||||
`conversation_id` VARCHAR(50) NOT NULL COMMENT '会话ID',
|
||||
`user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
|
||||
`agent_id` VARCHAR(50) DEFAULT NULL COMMENT '智能体ID',
|
||||
`role` VARCHAR(20) NOT NULL COMMENT '角色(user用户 assistant助手 system系统)',
|
||||
`content` LONGTEXT NOT NULL COMMENT '消息内容',
|
||||
`file_ids` VARCHAR(500) DEFAULT NULL COMMENT '关联文件ID(JSON数组)',
|
||||
@@ -103,12 +106,19 @@ CREATE TABLE `tb_ai_message` (
|
||||
`knowledge_refs` TEXT DEFAULT NULL COMMENT '知识库引用详情(JSON数组,包含title/snippet/score)',
|
||||
`token_count` INT(11) DEFAULT 0 COMMENT 'Token数量',
|
||||
`dify_message_id` VARCHAR(100) DEFAULT NULL COMMENT 'Dify消息ID',
|
||||
`rating` INT(4) DEFAULT NULL COMMENT '评分(1好评 -1差评 0取消评价)',
|
||||
`feedback` VARCHAR(1000) DEFAULT NULL COMMENT '反馈内容',
|
||||
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
`delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
|
||||
`deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_conversation_createtime` (`conversation_id`, `create_time` ASC),
|
||||
KEY `idx_user` (`user_id`),
|
||||
KEY `idx_agent` (`agent_id`),
|
||||
KEY `idx_role` (`role`),
|
||||
KEY `idx_create_time` (`create_time`),
|
||||
KEY `idx_deleted` (`deleted`),
|
||||
FULLTEXT KEY `ft_content` (`content`) WITH PARSER ngram
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='对话消息表(支持全文检索)';
|
||||
|
||||
@@ -132,13 +142,16 @@ CREATE TABLE `tb_ai_upload_file` (
|
||||
`error_message` VARCHAR(500) DEFAULT NULL COMMENT '错误信息',
|
||||
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '上传时间',
|
||||
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
`delete_time` TIMESTAMP NULL DEFAULT NULL COMMENT '删除时间',
|
||||
`deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否删除',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_user` (`user_id`),
|
||||
KEY `idx_knowledge` (`knowledge_id`),
|
||||
KEY `idx_conversation` (`conversation_id`),
|
||||
KEY `idx_dify_document` (`dify_document_id`),
|
||||
KEY `idx_status` (`status`),
|
||||
KEY `idx_create_time` (`create_time`)
|
||||
KEY `idx_create_time` (`create_time`),
|
||||
KEY `idx_deleted` (`deleted`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='上传文件表';
|
||||
|
||||
-- AI使用统计表
|
||||
|
||||
@@ -4,8 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.xyzh.ai.client.dto.*;
|
||||
@@ -26,10 +27,11 @@ import java.util.concurrent.TimeUnit;
|
||||
* @copyright xyzh
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DifyApiClient {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DifyApiClient.class);
|
||||
|
||||
@Autowired
|
||||
private DifyConfig difyConfig;
|
||||
|
||||
@@ -55,7 +57,7 @@ public class DifyApiClient {
|
||||
.retryOnConnectionFailure(false) // 流式不重试
|
||||
.build();
|
||||
|
||||
log.info("DifyApiClient初始化完成,API地址: {}", difyConfig.getApiBaseUrl());
|
||||
logger.info("DifyApiClient初始化完成,API地址: {}", difyConfig.getApiBaseUrl());
|
||||
}
|
||||
|
||||
// ===================== 知识库管理 API =====================
|
||||
@@ -79,14 +81,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("创建知识库失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("创建知识库失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("创建知识库失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, DatasetCreateResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("创建知识库异常", e);
|
||||
logger.error("创建知识库异常", e);
|
||||
throw new DifyException("创建知识库异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -108,14 +110,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("查询知识库列表失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("查询知识库列表失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("查询知识库列表失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, DatasetListResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("查询知识库列表异常", e);
|
||||
logger.error("查询知识库列表异常", e);
|
||||
throw new DifyException("查询知识库列表异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -137,14 +139,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("查询知识库详情失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("查询知识库详情失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("查询知识库详情失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, DatasetDetailResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("查询知识库详情异常", e);
|
||||
logger.error("查询知识库详情异常", e);
|
||||
throw new DifyException("查询知识库详情异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -168,13 +170,13 @@ public class DifyApiClient {
|
||||
try (Response response = httpClient.newCall(httpRequest).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
log.error("更新知识库失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("更新知识库失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("更新知识库失败: " + responseBody);
|
||||
}
|
||||
log.info("知识库更新成功: {}", datasetId);
|
||||
logger.info("知识库更新成功: {}", datasetId);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("更新知识库异常", e);
|
||||
logger.error("更新知识库异常", e);
|
||||
throw new DifyException("更新知识库异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -194,13 +196,13 @@ public class DifyApiClient {
|
||||
try (Response response = httpClient.newCall(httpRequest).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
log.error("删除知识库失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("删除知识库失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("删除知识库失败: " + responseBody);
|
||||
}
|
||||
log.info("知识库删除成功: {}", datasetId);
|
||||
logger.info("知识库删除成功: {}", datasetId);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("删除知识库异常", e);
|
||||
logger.error("删除知识库异常", e);
|
||||
throw new DifyException("删除知识库异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -246,14 +248,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("上传文档失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("上传文档失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("上传文档失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, DocumentUploadResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("上传文档异常", e);
|
||||
logger.error("上传文档异常", e);
|
||||
throw new DifyException("上传文档异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -275,14 +277,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("查询文档状态失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("查询文档状态失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("查询文档状态失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, DocumentStatusResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("查询文档状态异常", e);
|
||||
logger.error("查询文档状态异常", e);
|
||||
throw new DifyException("查询文档状态异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -304,14 +306,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("查询文档列表失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("查询文档列表失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("查询文档列表失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, DocumentListResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("查询文档列表异常", e);
|
||||
logger.error("查询文档列表异常", e);
|
||||
throw new DifyException("查询文档列表异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -332,13 +334,13 @@ public class DifyApiClient {
|
||||
try (Response response = httpClient.newCall(httpRequest).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
log.error("删除文档失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("删除文档失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("删除文档失败: " + responseBody);
|
||||
}
|
||||
log.info("文档删除成功: {}", documentId);
|
||||
logger.info("文档删除成功: {}", documentId);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("删除文档异常", e);
|
||||
logger.error("删除文档异常", e);
|
||||
throw new DifyException("删除文档异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -364,14 +366,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("知识库检索失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("知识库检索失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("知识库检索失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, RetrievalResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("知识库检索异常", e);
|
||||
logger.error("知识库检索异常", e);
|
||||
throw new DifyException("知识库检索异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -423,6 +425,9 @@ public class DifyApiClient {
|
||||
JsonNode jsonNode = objectMapper.readTree(data);
|
||||
String event = jsonNode.has("event") ? jsonNode.get("event").asText() : "";
|
||||
|
||||
// 转发所有事件到回调(包含完整数据)
|
||||
callback.onEvent(event, data);
|
||||
|
||||
switch (event) {
|
||||
case "message":
|
||||
case "agent_message":
|
||||
@@ -441,25 +446,27 @@ public class DifyApiClient {
|
||||
jsonNode.get("message").asText() : "未知错误";
|
||||
callback.onError(new DifyException(errorMsg));
|
||||
return;
|
||||
// 其他事件(workflow_started、node_started、node_finished等)
|
||||
// 已通过onEvent转发,这里不需要额外处理
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("流式响应处理异常", e);
|
||||
logger.error("流式响应处理异常", e);
|
||||
callback.onError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
log.error("流式对话请求失败", e);
|
||||
logger.error("流式对话请求失败", e);
|
||||
callback.onError(e);
|
||||
}
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("流式对话异常", e);
|
||||
logger.error("流式对话异常", e);
|
||||
callback.onError(e);
|
||||
}
|
||||
}
|
||||
@@ -486,14 +493,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("阻塞式对话失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("阻塞式对话失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("阻塞式对话失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, ChatResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("阻塞式对话异常", e);
|
||||
logger.error("阻塞式对话异常", e);
|
||||
throw new DifyException("阻塞式对话异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -516,17 +523,57 @@ public class DifyApiClient {
|
||||
try (Response response = httpClient.newCall(httpRequest).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
log.error("停止对话失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("停止对话失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("停止对话失败: " + responseBody);
|
||||
}
|
||||
log.info("对话停止成功: {}", taskId);
|
||||
logger.info("对话停止成功: {}", taskId);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("停止对话异常", e);
|
||||
logger.error("停止对话异常", e);
|
||||
throw new DifyException("停止对话异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交消息反馈
|
||||
* @param messageId Dify消息ID
|
||||
* @param rating 评分(like/dislike/null)
|
||||
* @param userId 用户ID
|
||||
* @param apiKey API密钥
|
||||
*/
|
||||
public void submitMessageFeedback(String messageId, String rating, String userId, String feedback, String apiKey) {
|
||||
String url = difyConfig.getFullApiUrl("/messages/" + messageId + "/feedbacks");
|
||||
|
||||
try {
|
||||
FeedbackRequest feedbackRequest = new FeedbackRequest(rating, userId, feedback);
|
||||
String jsonBody = objectMapper.writeValueAsString(feedbackRequest);
|
||||
|
||||
Request httpRequest = new Request.Builder()
|
||||
.url(url)
|
||||
.header("Authorization", "Bearer " + getApiKey(apiKey))
|
||||
.header("Content-Type", "application/json")
|
||||
.post(RequestBody.create(jsonBody, MediaType.parse("application/json")))
|
||||
.build();
|
||||
|
||||
try (Response response = httpClient.newCall(httpRequest).execute()) {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
logger.error("提交消息反馈失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("提交消息反馈失败: " + responseBody);
|
||||
}
|
||||
if (responseBody!="success") {
|
||||
logger.error("提交消息反馈失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("提交消息反馈失败: " + responseBody);
|
||||
}
|
||||
logger.info("消息反馈提交成功: {} - {}", messageId, rating);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("提交消息反馈异常", e);
|
||||
throw new DifyException("提交消息反馈异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// ===================== 对话历史 API =====================
|
||||
|
||||
/**
|
||||
@@ -563,14 +610,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("获取对话历史失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("获取对话历史失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("获取对话历史失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, MessageHistoryResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("获取对话历史异常", e);
|
||||
logger.error("获取对话历史异常", e);
|
||||
throw new DifyException("获取对话历史异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -605,14 +652,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("获取对话列表失败: {} - {}", response.code(), responseBody);
|
||||
logger.error("获取对话列表失败: {} - {}", response.code(), responseBody);
|
||||
throw new DifyException("获取对话列表失败: " + responseBody);
|
||||
}
|
||||
|
||||
return objectMapper.readValue(responseBody, ConversationListResponse.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("获取对话列表异常", e);
|
||||
logger.error("获取对话列表异常", e);
|
||||
throw new DifyException("获取对话列表异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -639,14 +686,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("GET请求失败: {} - {} - {}", url, response.code(), responseBody);
|
||||
logger.error("GET请求失败: {} - {} - {}", url, response.code(), responseBody);
|
||||
throw new DifyException("GET请求失败[" + response.code() + "]: " + responseBody);
|
||||
}
|
||||
|
||||
return responseBody;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("GET请求异常: {}", url, e);
|
||||
logger.error("GET请求异常: {}", url, e);
|
||||
throw new DifyException("GET请求异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -676,14 +723,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("POST请求失败: {} - {} - {}", url, response.code(), responseBody);
|
||||
logger.error("POST请求失败: {} - {} - {}", url, response.code(), responseBody);
|
||||
throw new DifyException("POST请求失败[" + response.code() + "]: " + responseBody);
|
||||
}
|
||||
|
||||
return responseBody;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("POST请求异常: {}", url, e);
|
||||
logger.error("POST请求异常: {}", url, e);
|
||||
throw new DifyException("POST请求异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -713,14 +760,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("PATCH请求失败: {} - {} - {}", url, response.code(), responseBody);
|
||||
logger.error("PATCH请求失败: {} - {} - {}", url, response.code(), responseBody);
|
||||
throw new DifyException("PATCH请求失败[" + response.code() + "]: " + responseBody);
|
||||
}
|
||||
|
||||
return responseBody;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("PATCH请求异常: {}", url, e);
|
||||
logger.error("PATCH请求异常: {}", url, e);
|
||||
throw new DifyException("PATCH请求异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -745,14 +792,14 @@ public class DifyApiClient {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
log.error("DELETE请求失败: {} - {} - {}", url, response.code(), responseBody);
|
||||
logger.error("DELETE请求失败: {} - {} - {}", url, response.code(), responseBody);
|
||||
throw new DifyException("DELETE请求失败[" + response.code() + "]: " + responseBody);
|
||||
}
|
||||
|
||||
return responseBody;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("DELETE请求异常: {}", url, e);
|
||||
logger.error("DELETE请求异常: {}", url, e);
|
||||
throw new DifyException("DELETE请求异常: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
@@ -790,5 +837,45 @@ public class DifyApiClient {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 反馈请求的内部类
|
||||
*/
|
||||
private static class FeedbackRequest {
|
||||
private String rating;
|
||||
private String user;
|
||||
|
||||
private String feedback;
|
||||
|
||||
public FeedbackRequest(String rating, String user, String feedback) {
|
||||
this.rating = rating;
|
||||
this.user = user;
|
||||
this.feedback = feedback;
|
||||
}
|
||||
|
||||
public String getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(String rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getFeedback() {
|
||||
return feedback;
|
||||
}
|
||||
|
||||
public void setFeedback(String feedback) {
|
||||
this.feedback = feedback;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,15 @@ public interface StreamCallback {
|
||||
*/
|
||||
void onMessageEnd(String metadata);
|
||||
|
||||
/**
|
||||
* 接收到Dify原始事件(用于转发完整事件数据)
|
||||
* @param eventType 事件类型(如workflow_started、node_started等)
|
||||
* @param eventData 完整的事件JSON数据
|
||||
*/
|
||||
default void onEvent(String eventType, String eventData) {
|
||||
// 默认实现:不处理
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式响应完成
|
||||
*/
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.xyzh.common.core.page.PageRequest;
|
||||
import org.xyzh.common.dto.ai.TbAiAgentConfig;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @description AI智能体配置控制器
|
||||
@@ -61,7 +62,7 @@ public class AiAgentConfigController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public ResultDomain<Boolean> deleteAgent(@PathVariable String id) {
|
||||
public ResultDomain<Boolean> deleteAgent(@PathVariable(name = "id") String id) {
|
||||
log.info("删除智能体: id={}", id);
|
||||
return agentConfigService.deleteAgent(id);
|
||||
}
|
||||
@@ -74,19 +75,19 @@ public class AiAgentConfigController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public ResultDomain<TbAiAgentConfig> getAgent(@PathVariable String id) {
|
||||
public ResultDomain<TbAiAgentConfig> getAgent(@PathVariable(name = "id") String id) {
|
||||
log.info("获取智能体: id={}", id);
|
||||
return agentConfigService.getAgentById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取启用的智能体列表
|
||||
* @return ResultDomain<List<TbAiAgentConfig>>
|
||||
* @return ResultDomain<TbAiAgentConfig>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/enabled")
|
||||
public ResultDomain<List<TbAiAgentConfig>> getEnabledAgents() {
|
||||
public ResultDomain<TbAiAgentConfig> getEnabledAgents() {
|
||||
log.info("获取启用的智能体列表");
|
||||
return agentConfigService.listEnabledAgents();
|
||||
}
|
||||
@@ -94,12 +95,12 @@ public class AiAgentConfigController {
|
||||
/**
|
||||
* @description 查询智能体列表
|
||||
* @param agentConfig 智能体配置
|
||||
* @return ResultDomain<List<TbAiAgentConfig>>
|
||||
* @return ResultDomain<TbAiAgentConfig>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PostMapping("/list")
|
||||
public ResultDomain<List<TbAiAgentConfig>> listAgents(
|
||||
public ResultDomain<TbAiAgentConfig> listAgents(
|
||||
@RequestBody TbAiAgentConfig agentConfig) {
|
||||
log.info("查询智能体列表: agentConfig={}", agentConfig);
|
||||
return agentConfigService.listAgents(agentConfig);
|
||||
@@ -121,34 +122,31 @@ public class AiAgentConfigController {
|
||||
|
||||
/**
|
||||
* @description 更新智能体状态
|
||||
* @param id 智能体ID
|
||||
* @param status 状态(0禁用 1启用)
|
||||
* @param requestBody 请求体(id, status)
|
||||
* @return ResultDomain<Boolean>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PutMapping("/{id}/status")
|
||||
public ResultDomain<Boolean> updateStatus(
|
||||
@PathVariable String id,
|
||||
@RequestParam Integer status) {
|
||||
@PutMapping("/status")
|
||||
public ResultDomain<Boolean> updateStatus(@RequestBody Map<String, Object> requestBody) {
|
||||
String id = (String) requestBody.get("id");
|
||||
Integer status = (Integer) requestBody.get("status");
|
||||
log.info("更新智能体状态: id={}, status={}", id, status);
|
||||
return agentConfigService.updateAgentStatus(id, status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 更新Dify配置
|
||||
* @param id 智能体ID
|
||||
* @param difyAppId Dify应用ID
|
||||
* @param difyApiKey Dify API Key
|
||||
* @param requestBody 请求体(id, difyAppId, difyApiKey)
|
||||
* @return ResultDomain<Boolean>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PutMapping("/{id}/dify")
|
||||
public ResultDomain<Boolean> updateDifyConfig(
|
||||
@PathVariable String id,
|
||||
@RequestParam String difyAppId,
|
||||
@RequestParam String difyApiKey) {
|
||||
@PutMapping("/dify")
|
||||
public ResultDomain<Boolean> updateDifyConfig(@RequestBody Map<String, Object> requestBody) {
|
||||
String id = (String) requestBody.get("id");
|
||||
String difyAppId = (String) requestBody.get("difyAppId");
|
||||
String difyApiKey = (String) requestBody.get("difyApiKey");
|
||||
log.info("更新Dify配置: id={}, difyAppId={}", id, difyAppId);
|
||||
return agentConfigService.updateDifyConfig(id, difyAppId, difyApiKey);
|
||||
}
|
||||
@@ -163,8 +161,8 @@ public class AiAgentConfigController {
|
||||
*/
|
||||
@GetMapping("/check-name")
|
||||
public ResultDomain<Boolean> checkNameExists(
|
||||
@RequestParam String name,
|
||||
@RequestParam(required = false) String excludeId) {
|
||||
@RequestParam(name = "name") String name,
|
||||
@RequestParam(name = "excludeId", required = false) String excludeId) {
|
||||
log.info("检查名称是否存在: name={}, excludeId={}", name, excludeId);
|
||||
return agentConfigService.checkNameExists(name, excludeId);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
import org.xyzh.api.ai.chat.AiChatService;
|
||||
import org.xyzh.api.ai.history.AiChatHistoryService;
|
||||
import org.xyzh.common.core.domain.ResultDomain;
|
||||
@@ -12,6 +13,7 @@ import org.xyzh.common.core.page.PageParam;
|
||||
import org.xyzh.common.dto.ai.TbAiConversation;
|
||||
import org.xyzh.common.dto.ai.TbAiMessage;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -37,22 +39,29 @@ public class AiChatController {
|
||||
|
||||
/**
|
||||
* @description 流式对话(SSE)
|
||||
* @param requestBody 请求体(agentId, conversationId, query, knowledgeIds)
|
||||
* @return ResultDomain<TbAiMessage>
|
||||
* @param agentId 智能体ID
|
||||
* @param conversationId 会话ID
|
||||
* @param query 用户问题
|
||||
* @param knowledgeIds 知识库ID列表(逗号分隔)
|
||||
* @return SseEmitter SSE流式推送对象
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public ResultDomain<TbAiMessage> streamChat(@RequestBody Map<String, Object> requestBody) {
|
||||
String agentId = (String) requestBody.get("agentId");
|
||||
String conversationId = (String) requestBody.get("conversationId");
|
||||
String query = (String) requestBody.get("query");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> knowledgeIds = (List<String>) requestBody.get("knowledgeIds");
|
||||
Object callback = requestBody.get("callback");
|
||||
@GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public SseEmitter streamChat(
|
||||
@RequestParam(name = "agentId") String agentId,
|
||||
@RequestParam(name = "conversationId", required = false) String conversationId,
|
||||
@RequestParam(name = "query") String query,
|
||||
@RequestParam(name = "knowledgeIds", required = false) String knowledgeIds) {
|
||||
|
||||
// 解析knowledgeIds
|
||||
List<String> knowledgeIdList = null;
|
||||
if (knowledgeIds != null && !knowledgeIds.isEmpty()) {
|
||||
knowledgeIdList = Arrays.asList(knowledgeIds.split(","));
|
||||
}
|
||||
|
||||
log.info("流式对话: agentId={}, conversationId={}, query={}", agentId, conversationId, query);
|
||||
return chatService.streamChat(agentId, conversationId, query, knowledgeIds, callback);
|
||||
return chatService.streamChatWithSse(agentId, conversationId, query, knowledgeIdList);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,34 +83,33 @@ public class AiChatController {
|
||||
return chatService.blockingChat(agentId, conversationId, query, knowledgeIds);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description 停止对话生成
|
||||
* @param messageId 消息ID
|
||||
* @description 停止对话生成(通过Dify TaskID)
|
||||
* @param requestBody 请求体(taskId, agentId)
|
||||
* @return ResultDomain<Boolean>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
* @since 2025-11-05
|
||||
*/
|
||||
@PostMapping("/stop/{messageId}")
|
||||
public ResultDomain<Boolean> stopChat(@PathVariable String messageId) {
|
||||
log.info("停止对话生成: messageId={}", messageId);
|
||||
return chatService.stopChat(messageId);
|
||||
@PostMapping("/stop-by-taskid")
|
||||
public ResultDomain<Boolean> stopChatByTaskId(@RequestBody Map<String, Object> requestBody) {
|
||||
String taskId = (String) requestBody.get("taskId");
|
||||
String agentId = (String) requestBody.get("agentId");
|
||||
log.info("停止对话生成(通过TaskID): taskId={}, agentId={}", taskId, agentId);
|
||||
return chatService.stopChatByTaskId(taskId, agentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 重新生成回答
|
||||
* @description 重新生成回答(SSE流式)
|
||||
* @param messageId 原消息ID
|
||||
* @param requestBody 请求体(可包含callback)
|
||||
* @return ResultDomain<TbAiMessage>
|
||||
* @return SseEmitter SSE流式推送对象
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
* @since 2025-11-05
|
||||
*/
|
||||
@PostMapping("/regenerate/{messageId}")
|
||||
public ResultDomain<TbAiMessage> regenerateAnswer(
|
||||
@PathVariable String messageId,
|
||||
@RequestBody(required = false) Map<String, Object> requestBody) {
|
||||
@GetMapping(value = "/regenerate/{messageId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public SseEmitter regenerateAnswer(@PathVariable(name = "messageId") String messageId) {
|
||||
log.info("重新生成回答: messageId={}", messageId);
|
||||
Object callback = requestBody != null ? requestBody.get("callback") : null;
|
||||
return chatService.regenerateAnswer(messageId, callback);
|
||||
return chatService.regenerateAnswerWithSse(messageId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,7 +122,7 @@ public class AiChatController {
|
||||
*/
|
||||
@PostMapping("/message/{messageId}/rate")
|
||||
public ResultDomain<Boolean> rateMessage(
|
||||
@PathVariable String messageId,
|
||||
@PathVariable(name = "messageId") String messageId,
|
||||
@RequestBody Map<String, Object> requestBody) {
|
||||
Integer rating = (Integer) requestBody.get("rating");
|
||||
String feedback = (String) requestBody.get("feedback");
|
||||
@@ -148,7 +156,7 @@ public class AiChatController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/conversation/{conversationId}")
|
||||
public ResultDomain<TbAiConversation> getConversation(@PathVariable String conversationId) {
|
||||
public ResultDomain<TbAiConversation> getConversation(@PathVariable(name = "conversationId") String conversationId) {
|
||||
log.info("获取会话信息: conversationId={}", conversationId);
|
||||
return chatService.getConversation(conversationId);
|
||||
}
|
||||
@@ -174,7 +182,7 @@ public class AiChatController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@DeleteMapping("/conversation/{conversationId}")
|
||||
public ResultDomain<Boolean> deleteConversation(@PathVariable String conversationId) {
|
||||
public ResultDomain<Boolean> deleteConversation(@PathVariable(name = "conversationId") String conversationId) {
|
||||
log.info("删除会话: conversationId={}", conversationId);
|
||||
return chatService.deleteConversation(conversationId);
|
||||
}
|
||||
@@ -182,13 +190,13 @@ public class AiChatController {
|
||||
/**
|
||||
* @description 获取用户的会话列表
|
||||
* @param agentId 智能体ID(可选)
|
||||
* @return ResultDomain<List<TbAiConversation>>
|
||||
* @return ResultDomain<TbAiConversation>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/conversations")
|
||||
public ResultDomain<List<TbAiConversation>> listUserConversations(
|
||||
@RequestParam(required = false) String agentId) {
|
||||
public ResultDomain<TbAiConversation> listUserConversations(
|
||||
@RequestParam(name = "agentId", required = false) String agentId) {
|
||||
log.info("获取用户会话列表: agentId={}", agentId);
|
||||
return chatService.listUserConversations(agentId);
|
||||
}
|
||||
@@ -198,12 +206,12 @@ public class AiChatController {
|
||||
/**
|
||||
* @description 获取会话的消息列表
|
||||
* @param conversationId 会话ID
|
||||
* @return ResultDomain<List<TbAiMessage>>
|
||||
* @return ResultDomain<TbAiMessage>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/conversation/{conversationId}/messages")
|
||||
public ResultDomain<List<TbAiMessage>> listMessages(@PathVariable String conversationId) {
|
||||
public ResultDomain<TbAiMessage> listMessages(@PathVariable(name = "conversationId") String conversationId) {
|
||||
log.info("获取会话消息列表: conversationId={}", conversationId);
|
||||
return chatService.listMessages(conversationId);
|
||||
}
|
||||
@@ -216,7 +224,7 @@ public class AiChatController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/message/{messageId}")
|
||||
public ResultDomain<TbAiMessage> getMessage(@PathVariable String messageId) {
|
||||
public ResultDomain<TbAiMessage> getMessage(@PathVariable(name = "messageId") String messageId) {
|
||||
log.info("获取消息: messageId={}", messageId);
|
||||
return chatService.getMessage(messageId);
|
||||
}
|
||||
@@ -229,7 +237,7 @@ public class AiChatController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PostMapping("/conversation/{conversationId}/summary")
|
||||
public ResultDomain<Boolean> generateSummary(@PathVariable String conversationId) {
|
||||
public ResultDomain<Boolean> generateSummary(@PathVariable(name = "conversationId") String conversationId) {
|
||||
log.info("生成会话摘要: conversationId={}", conversationId);
|
||||
return chatService.generateSummaryAsync(conversationId);
|
||||
}
|
||||
@@ -264,7 +272,7 @@ public class AiChatController {
|
||||
*/
|
||||
@PostMapping("/history/search")
|
||||
public PageDomain<TbAiConversation> searchConversations(
|
||||
@RequestParam String keyword,
|
||||
@RequestParam(name = "keyword") String keyword,
|
||||
@RequestBody PageParam pageParam) {
|
||||
log.info("搜索会话: keyword={}", keyword);
|
||||
return chatHistoryService.searchConversations(keyword, pageParam);
|
||||
@@ -272,32 +280,30 @@ public class AiChatController {
|
||||
|
||||
/**
|
||||
* @description 收藏/取消收藏会话
|
||||
* @param conversationId 会话ID
|
||||
* @param isFavorite 是否收藏
|
||||
* @param requestBody 请求体(conversationId, isFavorite)
|
||||
* @return ResultDomain<Boolean>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PutMapping("/history/conversation/{conversationId}/favorite")
|
||||
public ResultDomain<Boolean> toggleFavorite(
|
||||
@PathVariable String conversationId,
|
||||
@RequestParam Boolean isFavorite) {
|
||||
@PutMapping("/history/conversation/favorite")
|
||||
public ResultDomain<Boolean> toggleFavorite(@RequestBody Map<String, Object> requestBody) {
|
||||
String conversationId = (String) requestBody.get("conversationId");
|
||||
Boolean isFavorite = (Boolean) requestBody.get("isFavorite");
|
||||
log.info("{}收藏会话: conversationId={}", isFavorite ? "添加" : "取消", conversationId);
|
||||
return chatHistoryService.toggleFavorite(conversationId, isFavorite);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 置顶/取消置顶会话
|
||||
* @param conversationId 会话ID
|
||||
* @param isPinned 是否置顶
|
||||
* @param requestBody 请求体(conversationId, isPinned)
|
||||
* @return ResultDomain<Boolean>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PutMapping("/history/conversation/{conversationId}/pin")
|
||||
public ResultDomain<Boolean> togglePin(
|
||||
@PathVariable String conversationId,
|
||||
@RequestParam Boolean isPinned) {
|
||||
@PutMapping("/history/conversation/pin")
|
||||
public ResultDomain<Boolean> togglePin(@RequestBody Map<String, Object> requestBody) {
|
||||
String conversationId = (String) requestBody.get("conversationId");
|
||||
Boolean isPinned = (Boolean) requestBody.get("isPinned");
|
||||
log.info("{}置顶会话: conversationId={}", isPinned ? "添加" : "取消", conversationId);
|
||||
return chatHistoryService.togglePin(conversationId, isPinned);
|
||||
}
|
||||
@@ -325,7 +331,7 @@ public class AiChatController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/history/export/markdown/{conversationId}")
|
||||
public ResultDomain<String> exportAsMarkdown(@PathVariable String conversationId) {
|
||||
public ResultDomain<String> exportAsMarkdown(@PathVariable(name = "conversationId") String conversationId) {
|
||||
log.info("导出会话(Markdown): conversationId={}", conversationId);
|
||||
return chatHistoryService.exportConversationAsMarkdown(conversationId);
|
||||
}
|
||||
@@ -338,7 +344,7 @@ public class AiChatController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/history/export/json/{conversationId}")
|
||||
public ResultDomain<String> exportAsJson(@PathVariable String conversationId) {
|
||||
public ResultDomain<String> exportAsJson(@PathVariable(name = "conversationId") String conversationId) {
|
||||
log.info("导出会话(JSON): conversationId={}", conversationId);
|
||||
return chatHistoryService.exportConversationAsJson(conversationId);
|
||||
}
|
||||
@@ -346,13 +352,13 @@ public class AiChatController {
|
||||
/**
|
||||
* @description 获取最近对话列表
|
||||
* @param limit 限制数量(可选,默认10)
|
||||
* @return ResultDomain<List<TbAiConversation>>
|
||||
* @return ResultDomain<TbAiConversation>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/history/recent")
|
||||
public ResultDomain<TbAiConversation> getRecentConversations(
|
||||
@RequestParam(defaultValue = "10") Integer limit) {
|
||||
@RequestParam(name = "limit", defaultValue = "10") Integer limit) {
|
||||
log.info("获取最近对话列表: limit={}", limit);
|
||||
return chatHistoryService.getRecentConversations(limit);
|
||||
}
|
||||
@@ -366,7 +372,7 @@ public class AiChatController {
|
||||
*/
|
||||
@GetMapping("/history/statistics")
|
||||
public ResultDomain<Map<String, Object>> getUserChatStatistics(
|
||||
@RequestParam(required = false) String userId) {
|
||||
@RequestParam(name = "userId", required = false) String userId) {
|
||||
log.info("获取用户对话统计: userId={}", userId);
|
||||
return chatHistoryService.getUserChatStatistics(userId);
|
||||
}
|
||||
@@ -379,7 +385,7 @@ public class AiChatController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/history/conversation/{conversationId}/statistics")
|
||||
public ResultDomain<Map<String, Object>> getConversationStatistics(@PathVariable String conversationId) {
|
||||
public ResultDomain<Map<String, Object>> getConversationStatistics(@PathVariable(name = "conversationId") String conversationId) {
|
||||
log.info("获取会话统计: conversationId={}", conversationId);
|
||||
return chatHistoryService.getConversationStatistics(conversationId);
|
||||
}
|
||||
|
||||
@@ -40,9 +40,9 @@ public class AiFileUploadController {
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
public ResultDomain<TbAiUploadFile> uploadFile(
|
||||
@RequestParam String knowledgeId,
|
||||
@RequestParam("file") MultipartFile file,
|
||||
@RequestParam(required = false) String indexingTechnique) {
|
||||
@RequestParam(name = "knowledgeId") String knowledgeId,
|
||||
@RequestParam(name = "file") MultipartFile file,
|
||||
@RequestParam(name = "indexingTechnique", required = false) String indexingTechnique) {
|
||||
log.info("上传文件到知识库: knowledgeId={}, fileName={}", knowledgeId, file.getOriginalFilename());
|
||||
return uploadFileService.uploadToKnowledge(knowledgeId, file, indexingTechnique);
|
||||
}
|
||||
@@ -52,15 +52,15 @@ public class AiFileUploadController {
|
||||
* @param knowledgeId 知识库ID
|
||||
* @param files 文件列表
|
||||
* @param indexingTechnique 索引方式(可选)
|
||||
* @return ResultDomain<List<TbAiUploadFile>>
|
||||
* @return ResultDomain<TbAiUploadFile>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PostMapping("/upload/batch")
|
||||
public ResultDomain<List<TbAiUploadFile>> batchUploadFiles(
|
||||
@RequestParam String knowledgeId,
|
||||
@RequestParam("files") MultipartFile[] files,
|
||||
@RequestParam(required = false) String indexingTechnique) {
|
||||
public ResultDomain<TbAiUploadFile> batchUploadFiles(
|
||||
@RequestParam(name = "knowledgeId") String knowledgeId,
|
||||
@RequestParam(name = "files") MultipartFile[] files,
|
||||
@RequestParam(name = "indexingTechnique", required = false) String indexingTechnique) {
|
||||
log.info("批量上传文件: knowledgeId={}, fileCount={}", knowledgeId, files.length);
|
||||
return uploadFileService.batchUploadToKnowledge(knowledgeId, Arrays.asList(files), indexingTechnique);
|
||||
}
|
||||
@@ -73,7 +73,7 @@ public class AiFileUploadController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/{fileId}")
|
||||
public ResultDomain<TbAiUploadFile> getFile(@PathVariable String fileId) {
|
||||
public ResultDomain<TbAiUploadFile> getFile(@PathVariable(name = "fileId") String fileId) {
|
||||
log.info("获取文件信息: fileId={}", fileId);
|
||||
return uploadFileService.getFileById(fileId);
|
||||
}
|
||||
@@ -81,12 +81,12 @@ public class AiFileUploadController {
|
||||
/**
|
||||
* @description 查询知识库的文件列表
|
||||
* @param knowledgeId 知识库ID
|
||||
* @return ResultDomain<List<TbAiUploadFile>>
|
||||
* @return ResultDomain<TbAiUploadFile>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public ResultDomain<List<TbAiUploadFile>> listFiles(@RequestParam String knowledgeId) {
|
||||
public ResultDomain<TbAiUploadFile> listFiles(@RequestParam(name = "knowledgeId") String knowledgeId) {
|
||||
log.info("查询知识库文件列表: knowledgeId={}", knowledgeId);
|
||||
return uploadFileService.listFilesByKnowledge(knowledgeId);
|
||||
}
|
||||
@@ -112,7 +112,7 @@ public class AiFileUploadController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@DeleteMapping("/{fileId}")
|
||||
public ResultDomain<Boolean> deleteFile(@PathVariable String fileId) {
|
||||
public ResultDomain<Boolean> deleteFile(@PathVariable(name = "fileId") String fileId) {
|
||||
log.info("删除文件: fileId={}", fileId);
|
||||
return uploadFileService.deleteFile(fileId);
|
||||
}
|
||||
@@ -125,7 +125,7 @@ public class AiFileUploadController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/{fileId}/status")
|
||||
public ResultDomain<TbAiUploadFile> getFileStatus(@PathVariable String fileId) {
|
||||
public ResultDomain<TbAiUploadFile> getFileStatus(@PathVariable(name = "fileId") String fileId) {
|
||||
log.info("查询文件处理状态: fileId={}", fileId);
|
||||
return uploadFileService.getFileStatus(fileId);
|
||||
}
|
||||
@@ -138,7 +138,7 @@ public class AiFileUploadController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PostMapping("/{fileId}/sync")
|
||||
public ResultDomain<TbAiUploadFile> syncFileStatus(@PathVariable String fileId) {
|
||||
public ResultDomain<TbAiUploadFile> syncFileStatus(@PathVariable(name = "fileId") String fileId) {
|
||||
log.info("同步文件状态: fileId={}", fileId);
|
||||
return uploadFileService.syncFileStatus(fileId);
|
||||
}
|
||||
@@ -146,12 +146,12 @@ public class AiFileUploadController {
|
||||
/**
|
||||
* @description 批量同步知识库的所有文件状态
|
||||
* @param knowledgeId 知识库ID
|
||||
* @return ResultDomain<List<TbAiUploadFile>>
|
||||
* @return ResultDomain<TbAiUploadFile>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PostMapping("/sync/knowledge/{knowledgeId}")
|
||||
public ResultDomain<List<TbAiUploadFile>> syncKnowledgeFiles(@PathVariable String knowledgeId) {
|
||||
public ResultDomain<TbAiUploadFile> syncKnowledgeFiles(@PathVariable(name = "knowledgeId") String knowledgeId) {
|
||||
log.info("批量同步知识库文件状态: knowledgeId={}", knowledgeId);
|
||||
return uploadFileService.syncKnowledgeFiles(knowledgeId);
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public class AiKnowledgeController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public ResultDomain<Boolean> deleteKnowledge(@PathVariable String id) {
|
||||
public ResultDomain<Boolean> deleteKnowledge(@PathVariable(name = "id") String id) {
|
||||
log.info("删除知识库: id={}", id);
|
||||
return knowledgeService.deleteKnowledge(id);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ public class AiKnowledgeController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public ResultDomain<TbAiKnowledge> getKnowledge(@PathVariable String id) {
|
||||
public ResultDomain<TbAiKnowledge> getKnowledge(@PathVariable(name = "id") String id) {
|
||||
log.info("获取知识库: id={}", id);
|
||||
return knowledgeService.getKnowledgeById(id);
|
||||
}
|
||||
@@ -90,12 +90,12 @@ public class AiKnowledgeController {
|
||||
/**
|
||||
* @description 查询知识库列表
|
||||
* @param filter 过滤条件
|
||||
* @return ResultDomain<List<TbAiKnowledge>>
|
||||
* @return ResultDomain<TbAiKnowledge>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PostMapping("/list")
|
||||
public ResultDomain<List<TbAiKnowledge>> listKnowledges(
|
||||
public ResultDomain<TbAiKnowledge> listKnowledges(
|
||||
@RequestBody(required = false) TbAiKnowledge filter) {
|
||||
log.info("查询知识库列表");
|
||||
return knowledgeService.listKnowledges(filter);
|
||||
@@ -122,23 +122,21 @@ public class AiKnowledgeController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PostMapping("/{id}/sync")
|
||||
public ResultDomain<TbAiKnowledge> syncFromDify(@PathVariable String id) {
|
||||
public ResultDomain<TbAiKnowledge> syncFromDify(@PathVariable(name = "id") String id) {
|
||||
log.info("同步Dify知识库信息: id={}", id);
|
||||
return knowledgeService.syncFromDify(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 更新知识库权限
|
||||
* @param knowledgeId 知识库ID
|
||||
* @param requestBody 请求体(permissionType, deptIds, roleIds)
|
||||
* @param requestBody 请求体(knowledgeId, permissionType, deptIds, roleIds)
|
||||
* @return ResultDomain<Boolean>
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@PutMapping("/{knowledgeId}/permission")
|
||||
public ResultDomain<Boolean> updatePermission(
|
||||
@PathVariable String knowledgeId,
|
||||
@RequestBody Map<String, Object> requestBody) {
|
||||
@PutMapping("/permission")
|
||||
public ResultDomain<Boolean> updatePermission(@RequestBody Map<String, Object> requestBody) {
|
||||
String knowledgeId = (String) requestBody.get("knowledgeId");
|
||||
String permissionType = (String) requestBody.get("permissionType");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> deptIds = (List<String>) requestBody.get("deptIds");
|
||||
@@ -159,8 +157,8 @@ public class AiKnowledgeController {
|
||||
*/
|
||||
@GetMapping("/{knowledgeId}/permission")
|
||||
public ResultDomain<Boolean> checkPermission(
|
||||
@PathVariable String knowledgeId,
|
||||
@RequestParam String operationType) {
|
||||
@PathVariable(name = "knowledgeId") String knowledgeId,
|
||||
@RequestParam(name = "operationType") String operationType) {
|
||||
log.info("检查知识库权限: knowledgeId={}, operationType={}", knowledgeId, operationType);
|
||||
return knowledgeService.checkKnowledgePermission(knowledgeId, operationType);
|
||||
}
|
||||
@@ -173,7 +171,7 @@ public class AiKnowledgeController {
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
@GetMapping("/{id}/stats")
|
||||
public ResultDomain<TbAiKnowledge> getKnowledgeStats(@PathVariable String id) {
|
||||
public ResultDomain<TbAiKnowledge> getKnowledgeStats(@PathVariable(name = "id") String id) {
|
||||
log.info("获取知识库统计信息: id={}", id);
|
||||
return knowledgeService.getKnowledgeStats(id);
|
||||
}
|
||||
|
||||
@@ -35,8 +35,8 @@ public class DifyProxyController {
|
||||
*/
|
||||
@GetMapping("/datasets/{datasetId}/documents/{documentId}/segments")
|
||||
public ResultDomain<String> getDocumentSegments(
|
||||
@PathVariable String datasetId,
|
||||
@PathVariable String documentId) {
|
||||
@PathVariable(name = "datasetId") String datasetId,
|
||||
@PathVariable(name = "documentId") String documentId) {
|
||||
ResultDomain<String> result = new ResultDomain<>();
|
||||
log.info("获取文档分段列表: datasetId={}, documentId={}", datasetId, documentId);
|
||||
|
||||
@@ -65,9 +65,9 @@ public class DifyProxyController {
|
||||
*/
|
||||
@GetMapping("/datasets/{datasetId}/documents/{documentId}/segments/{segmentId}/child_chunks")
|
||||
public ResultDomain<String> getChildChunks(
|
||||
@PathVariable String datasetId,
|
||||
@PathVariable String documentId,
|
||||
@PathVariable String segmentId) {
|
||||
@PathVariable(name = "datasetId") String datasetId,
|
||||
@PathVariable(name = "documentId") String documentId,
|
||||
@PathVariable(name = "segmentId") String segmentId) {
|
||||
|
||||
log.info("获取子块列表: datasetId={}, documentId={}, segmentId={}",
|
||||
datasetId, documentId, segmentId);
|
||||
@@ -101,10 +101,10 @@ public class DifyProxyController {
|
||||
*/
|
||||
@PatchMapping("/datasets/{datasetId}/documents/{documentId}/segments/{segmentId}/child_chunks/{childChunkId}")
|
||||
public ResultDomain<String> updateChildChunk(
|
||||
@PathVariable String datasetId,
|
||||
@PathVariable String documentId,
|
||||
@PathVariable String segmentId,
|
||||
@PathVariable String childChunkId,
|
||||
@PathVariable(name = "datasetId") String datasetId,
|
||||
@PathVariable(name = "documentId") String documentId,
|
||||
@PathVariable(name = "segmentId") String segmentId,
|
||||
@PathVariable(name = "childChunkId") String childChunkId,
|
||||
@RequestBody Map<String, Object> requestBody) {
|
||||
|
||||
log.info("更新子块: datasetId={}, documentId={}, segmentId={}, childChunkId={}",
|
||||
@@ -138,9 +138,9 @@ public class DifyProxyController {
|
||||
*/
|
||||
@PostMapping("/datasets/{datasetId}/documents/{documentId}/segments/{segmentId}/child_chunks")
|
||||
public ResultDomain<String> createChildChunk(
|
||||
@PathVariable String datasetId,
|
||||
@PathVariable String documentId,
|
||||
@PathVariable String segmentId,
|
||||
@PathVariable(name = "datasetId") String datasetId,
|
||||
@PathVariable(name = "documentId") String documentId,
|
||||
@PathVariable(name = "segmentId") String segmentId,
|
||||
@RequestBody Map<String, Object> requestBody) {
|
||||
|
||||
log.info("创建子块: datasetId={}, documentId={}, segmentId={}",
|
||||
@@ -174,10 +174,10 @@ public class DifyProxyController {
|
||||
*/
|
||||
@DeleteMapping("/datasets/{datasetId}/documents/{documentId}/segments/{segmentId}/child_chunks/{childChunkId}")
|
||||
public ResultDomain<String> deleteChildChunk(
|
||||
@PathVariable String datasetId,
|
||||
@PathVariable String documentId,
|
||||
@PathVariable String segmentId,
|
||||
@PathVariable String childChunkId) {
|
||||
@PathVariable(name = "datasetId") String datasetId,
|
||||
@PathVariable(name = "documentId") String documentId,
|
||||
@PathVariable(name = "segmentId") String segmentId,
|
||||
@PathVariable(name = "childChunkId") String childChunkId) {
|
||||
|
||||
log.info("删除子块: datasetId={}, documentId={}, segmentId={}, childChunkId={}",
|
||||
datasetId, documentId, segmentId, childChunkId);
|
||||
|
||||
@@ -240,8 +240,8 @@ public class AiAgentConfigServiceImpl implements AiAgentConfigService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<TbAiAgentConfig>> listEnabledAgents() {
|
||||
ResultDomain<List<TbAiAgentConfig>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<TbAiAgentConfig> listEnabledAgents() {
|
||||
ResultDomain<TbAiAgentConfig> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
TbAiAgentConfig filter = new TbAiAgentConfig();
|
||||
@@ -259,8 +259,8 @@ public class AiAgentConfigServiceImpl implements AiAgentConfigService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<TbAiAgentConfig>> listAgents(TbAiAgentConfig filter) {
|
||||
ResultDomain<List<TbAiAgentConfig>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<TbAiAgentConfig> listAgents(TbAiAgentConfig filter) {
|
||||
ResultDomain<TbAiAgentConfig> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
List<TbAiAgentConfig> agents = agentConfigMapper.selectAgentConfigs(filter);
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
import org.xyzh.ai.client.DifyApiClient;
|
||||
import org.xyzh.ai.client.callback.StreamCallback;
|
||||
import org.xyzh.ai.client.dto.ChatRequest;
|
||||
@@ -24,6 +25,7 @@ import org.xyzh.common.dto.ai.TbAiMessage;
|
||||
import org.xyzh.common.dto.user.TbSysUser;
|
||||
import org.xyzh.system.utils.LoginUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -58,48 +60,46 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
// 异步任务线程池
|
||||
// 异步任务线程池(用于异步生成摘要等后台任务)
|
||||
private final ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResultDomain<TbAiMessage> streamChat(
|
||||
String agentId,
|
||||
String conversationId,
|
||||
String query,
|
||||
List<String> knowledgeIds,
|
||||
Object callbackObj) {
|
||||
|
||||
ResultDomain<TbAiMessage> resultDomain = new ResultDomain<>();
|
||||
StreamCallback callback = (callbackObj instanceof StreamCallback) ? (StreamCallback) callbackObj : null;
|
||||
public SseEmitter streamChatWithSse(String agentId, String conversationId, String query, List<String> knowledgeIds) {
|
||||
// 创建SseEmitter,设置超时时间为5分钟
|
||||
SseEmitter emitter = new SseEmitter(5 * 60 * 1000L);
|
||||
|
||||
try {
|
||||
// 1. 参数验证
|
||||
if (!StringUtils.hasText(agentId)) {
|
||||
resultDomain.fail("智能体ID不能为空");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("智能体ID不能为空"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
if (!StringUtils.hasText(query)) {
|
||||
resultDomain.fail("问题不能为空");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("问题不能为空"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
|
||||
// 2. 获取当前用户
|
||||
// 2. 获取当前用户(在主线程中,可以正常获取)
|
||||
TbSysUser currentUser = LoginUtil.getCurrentUser();
|
||||
if (currentUser == null) {
|
||||
resultDomain.fail("用户未登录");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("用户未登录"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
|
||||
// 3. 查询智能体配置
|
||||
TbAiAgentConfig agent = agentConfigMapper.selectAgentConfigById(agentId);
|
||||
if (agent == null || agent.getDeleted()) {
|
||||
resultDomain.fail("智能体不存在");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("智能体不存在"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
if (agent.getStatus() != 1) {
|
||||
resultDomain.fail("智能体未启用");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("智能体未启用"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
|
||||
// 4. 获取或创建会话
|
||||
@@ -107,101 +107,94 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
if (StringUtils.hasText(conversationId)) {
|
||||
conversation = conversationMapper.selectConversationById(conversationId);
|
||||
if (conversation == null || conversation.getDeleted()) {
|
||||
resultDomain.fail("会话不存在");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("会话不存在"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
// 验证会话所属权
|
||||
if (!conversation.getUserID().equals(currentUser.getID())) {
|
||||
resultDomain.fail("无权访问此会话");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("无权访问此会话"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
} else {
|
||||
// 创建新会话
|
||||
ResultDomain<TbAiConversation> createResult = createConversation(agentId, null);
|
||||
if (!createResult.isSuccess()) {
|
||||
resultDomain.fail(createResult.getMessage());
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data(createResult.getMessage()));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
conversation = createResult.getData();
|
||||
conversationId = conversation.getID();
|
||||
}
|
||||
|
||||
final String finalConversationId = conversation.getID();
|
||||
|
||||
// 5. 创建用户消息记录
|
||||
TbAiMessage userMessage = new TbAiMessage();
|
||||
userMessage.setID(UUID.randomUUID().toString());
|
||||
userMessage.setConversationID(conversationId);
|
||||
userMessage.setConversationID(finalConversationId);
|
||||
userMessage.setAgentID(agentId);
|
||||
userMessage.setRole("user");
|
||||
userMessage.setContent(query);
|
||||
userMessage.setCreateTime(new Date());
|
||||
userMessage.setUpdateTime(new Date());
|
||||
userMessage.setDeleted(false);
|
||||
|
||||
userMessage.setUserID(currentUser.getID());
|
||||
messageMapper.insertMessage(userMessage);
|
||||
|
||||
// 6. 创建AI回复消息记录(初始为空)
|
||||
TbAiMessage aiMessage = new TbAiMessage();
|
||||
aiMessage.setID(UUID.randomUUID().toString());
|
||||
aiMessage.setConversationID(conversationId);
|
||||
aiMessage.setAgentID(agentId);
|
||||
aiMessage.setRole("assistant");
|
||||
aiMessage.setContent(""); // 初始为空,流式更新
|
||||
aiMessage.setCreateTime(new Date());
|
||||
aiMessage.setUpdateTime(new Date());
|
||||
aiMessage.setDeleted(false);
|
||||
|
||||
messageMapper.insertMessage(aiMessage);
|
||||
// 注意:AI消息记录将在获取到Dify的task_id后创建
|
||||
|
||||
// 7. 构建Dify请求
|
||||
ChatRequest chatRequest = new ChatRequest();
|
||||
chatRequest.setQuery(query);
|
||||
chatRequest.setUser(currentUser.getID());
|
||||
|
||||
// 设置会话ID(如果是继续对话)
|
||||
if (StringUtils.hasText(conversation.getDifyConversationId())) {
|
||||
chatRequest.setConversationId(conversation.getDifyConversationId());
|
||||
}
|
||||
|
||||
// 设置知识库检索(如果指定)
|
||||
if (knowledgeIds != null && !knowledgeIds.isEmpty()) {
|
||||
chatRequest.setDatasetIds(knowledgeIds);
|
||||
}
|
||||
|
||||
// 使用agent配置的参数
|
||||
if (agent.getTemperature() != null) {
|
||||
chatRequest.setTemperature(agent.getTemperature().doubleValue());
|
||||
} else {
|
||||
chatRequest.setTemperature(difyConfig.getChat().getDefaultTemperature());
|
||||
}
|
||||
chatRequest.setTemperature(agent.getTemperature() != null ?
|
||||
agent.getTemperature().doubleValue() : difyConfig.getChat().getDefaultTemperature());
|
||||
chatRequest.setMaxTokens(agent.getMaxTokens() != null ?
|
||||
agent.getMaxTokens() : difyConfig.getChat().getDefaultMaxTokens());
|
||||
|
||||
if (agent.getMaxTokens() != null) {
|
||||
chatRequest.setMaxTokens(agent.getMaxTokens());
|
||||
} else {
|
||||
chatRequest.setMaxTokens(difyConfig.getChat().getDefaultMaxTokens());
|
||||
}
|
||||
|
||||
// 8. 调用Dify流式对话
|
||||
final String finalConversationId = conversationId;
|
||||
final String finalAiMessageId = aiMessage.getID();
|
||||
// 6. 调用Dify流式对话
|
||||
final TbAiConversation finalConversation = conversation;
|
||||
StringBuilder fullAnswer = new StringBuilder();
|
||||
AtomicReference<String> difyConversationId = new AtomicReference<>();
|
||||
AtomicReference<String> difyMessageId = new AtomicReference<>();
|
||||
|
||||
try {
|
||||
AtomicReference<String> taskId = new AtomicReference<>(); // 用于存储Dify的task_id
|
||||
AtomicReference<Boolean> messageCreated = new AtomicReference<>(false); // 标记消息是否已创建
|
||||
AtomicReference<Boolean> isStopped = new AtomicReference<>(false); // 标记是否已停止
|
||||
difyApiClient.streamChat(chatRequest, agent.getDifyApiKey(), new StreamCallback() {
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
if (isStopped.get()) {
|
||||
return; // 已停止,不再处理
|
||||
}
|
||||
try {
|
||||
fullAnswer.append(message);
|
||||
// 转发给前端回调
|
||||
if (callback != null) {
|
||||
callback.onMessage(message);
|
||||
// 发送消息片段给前端
|
||||
emitter.send(SseEmitter.event().name("message").data(message));
|
||||
} catch (IllegalStateException e) {
|
||||
// SseEmitter已关闭(用户停止生成),标记为已停止
|
||||
isStopped.set(true);
|
||||
log.debug("SSE连接已关闭,停止发送消息");
|
||||
} catch (IOException e) {
|
||||
log.error("发送SSE消息失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageEnd(String metadata) {
|
||||
if (isStopped.get()) {
|
||||
return; // 已停止,不再处理
|
||||
}
|
||||
try {
|
||||
// 解析metadata获取会话ID和消息ID
|
||||
// 解析metadata
|
||||
JsonNode json = objectMapper.readTree(metadata);
|
||||
if (json.has("conversation_id")) {
|
||||
difyConversationId.set(json.get("conversation_id").asText());
|
||||
@@ -210,67 +203,161 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
difyMessageId.set(json.get("id").asText());
|
||||
}
|
||||
|
||||
// 更新AI消息内容
|
||||
// 更新AI消息内容(使用task_id作为消息ID)
|
||||
if (taskId.get() != null) {
|
||||
TbAiMessage updateMessage = new TbAiMessage();
|
||||
updateMessage.setID(finalAiMessageId);
|
||||
updateMessage.setID(taskId.get());
|
||||
updateMessage.setContent(fullAnswer.toString());
|
||||
updateMessage.setDifyMessageId(difyMessageId.get());
|
||||
updateMessage.setUpdateTime(new Date());
|
||||
messageMapper.updateMessage(updateMessage);
|
||||
}
|
||||
|
||||
// 更新会话的Dify会话ID
|
||||
if (StringUtils.hasText(difyConversationId.get())) {
|
||||
TbAiConversation updateConv = new TbAiConversation();
|
||||
updateConv.setID(finalConversationId);
|
||||
updateConv.setDifyConversationId(difyConversationId.get());
|
||||
updateConv.setMessageCount((conversation.getMessageCount() != null ?
|
||||
conversation.getMessageCount() : 0) + 2); // 用户问题+AI回答
|
||||
updateConv.setMessageCount((finalConversation.getMessageCount() != null ?
|
||||
finalConversation.getMessageCount() : 0) + 2);
|
||||
updateConv.setUpdateTime(new Date());
|
||||
conversationMapper.updateConversation(updateConv);
|
||||
}
|
||||
|
||||
if (callback != null) {
|
||||
callback.onMessageEnd(metadata);
|
||||
}
|
||||
|
||||
// 发送结束事件(保持兼容)
|
||||
emitter.send(SseEmitter.event().name("end").data(metadata));
|
||||
} catch (IllegalStateException e) {
|
||||
// SseEmitter已关闭,标记为已停止
|
||||
isStopped.set(true);
|
||||
log.debug("SSE连接已关闭,停止处理");
|
||||
} catch (Exception e) {
|
||||
log.error("处理流式响应metadata失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(String eventType, String eventData) {
|
||||
if (isStopped.get()) {
|
||||
return; // 已停止,不再处理
|
||||
}
|
||||
try {
|
||||
// 转发所有Dify原始事件到前端(包含task_id等完整信息)
|
||||
emitter.send(SseEmitter.event().name("dify_" + eventType).data(eventData));
|
||||
log.debug("转发Dify事件: {} - {}", eventType, eventData);
|
||||
|
||||
// 如果还没有创建消息记录,尝试从任何事件中提取task_id
|
||||
if (!messageCreated.get()) {
|
||||
JsonNode json = objectMapper.readTree(eventData);
|
||||
if (json.has("task_id")) {
|
||||
String difyTaskId = json.get("task_id").asText();
|
||||
|
||||
// 只有在taskId为空时才设置并创建消息
|
||||
if (taskId.get() == null) {
|
||||
taskId.set(difyTaskId);
|
||||
|
||||
// 使用task_id作为消息ID,创建AI消息记录
|
||||
TbAiMessage aiMessage = new TbAiMessage();
|
||||
aiMessage.setID(difyTaskId); // 使用Dify的task_id作为消息ID
|
||||
aiMessage.setConversationID(finalConversationId);
|
||||
aiMessage.setAgentID(agentId);
|
||||
aiMessage.setRole("assistant");
|
||||
aiMessage.setContent("");
|
||||
aiMessage.setCreateTime(new Date());
|
||||
aiMessage.setUpdateTime(new Date());
|
||||
aiMessage.setDeleted(false);
|
||||
aiMessage.setUserID("assistant");
|
||||
messageMapper.insertMessage(aiMessage);
|
||||
|
||||
messageCreated.set(true);
|
||||
log.info("AI消息记录已创建(从{}事件提取task_id): {}", eventType, difyTaskId);
|
||||
|
||||
// 发送init事件给前端,包含task_id(作为messageId)
|
||||
Map<String, String> initData = new HashMap<>();
|
||||
initData.put("conversationId", finalConversationId);
|
||||
initData.put("messageId", difyTaskId);
|
||||
emitter.send(SseEmitter.event().name("init").data(initData));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalStateException e) {
|
||||
// SseEmitter已关闭,标记为已停止
|
||||
isStopped.set(true);
|
||||
log.debug("SSE连接已关闭,停止转发事件");
|
||||
} catch (IOException e) {
|
||||
log.error("转发Dify事件失败: {}", eventType, e);
|
||||
} catch (Exception e) {
|
||||
log.error("处理Dify事件异常: {}", eventType, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
log.info("流式对话完成: {} - {}", finalConversationId, finalAiMessageId);
|
||||
if (callback != null) {
|
||||
callback.onComplete();
|
||||
if (isStopped.get()) {
|
||||
log.debug("SSE连接已关闭,跳过完成处理");
|
||||
return; // 已停止,不再处理
|
||||
}
|
||||
try {
|
||||
log.info("流式对话完成: {} - {}", finalConversationId, taskId.get());
|
||||
emitter.send(SseEmitter.event().name("complete").data("对话完成"));
|
||||
emitter.complete();
|
||||
} catch (IllegalStateException e) {
|
||||
// SseEmitter已关闭
|
||||
isStopped.set(true);
|
||||
log.debug("SSE连接已关闭");
|
||||
} catch (IOException e) {
|
||||
log.error("发送完成事件失败", e);
|
||||
emitter.completeWithError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable error) {
|
||||
if (isStopped.get()) {
|
||||
log.debug("SSE连接已关闭,跳过错误处理");
|
||||
return; // 已停止,不再处理
|
||||
}
|
||||
try {
|
||||
log.error("流式对话失败", error);
|
||||
if (callback != null) {
|
||||
callback.onError(error);
|
||||
emitter.send(SseEmitter.event().name("error").data(error.getMessage()));
|
||||
emitter.completeWithError(error);
|
||||
} catch (IllegalStateException e) {
|
||||
// SseEmitter已关闭
|
||||
isStopped.set(true);
|
||||
log.debug("SSE连接已关闭");
|
||||
} catch (IOException e) {
|
||||
log.error("发送错误事件失败", e);
|
||||
emitter.completeWithError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
resultDomain.success("对话成功", aiMessage);
|
||||
return resultDomain;
|
||||
// 设置超时和错误回调
|
||||
emitter.onTimeout(() -> {
|
||||
isStopped.set(true);
|
||||
log.warn("SSE连接超时");
|
||||
emitter.complete();
|
||||
});
|
||||
|
||||
} catch (DifyException e) {
|
||||
log.error("Dify对话失败", e);
|
||||
resultDomain.fail("对话失败: " + e.getMessage());
|
||||
return resultDomain;
|
||||
}
|
||||
emitter.onError(e -> {
|
||||
isStopped.set(true);
|
||||
log.error("SSE连接错误(可能是用户停止生成)", e);
|
||||
emitter.completeWithError(e);
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("流式对话异常", e);
|
||||
resultDomain.fail("对话异常: " + e.getMessage());
|
||||
return resultDomain;
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("error").data(e.getMessage()));
|
||||
emitter.completeWithError(e);
|
||||
} catch (IOException ex) {
|
||||
log.error("发送异常事件失败", ex);
|
||||
emitter.completeWithError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
return emitter;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResultDomain<TbAiMessage> blockingChat(
|
||||
@@ -396,24 +483,22 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<Boolean> stopChat(String messageId) {
|
||||
public ResultDomain<Boolean> stopChatByTaskId(String taskId, String agentId) {
|
||||
ResultDomain<Boolean> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
if (!StringUtils.hasText(messageId)) {
|
||||
resultDomain.fail("消息ID不能为空");
|
||||
if (!StringUtils.hasText(taskId)) {
|
||||
resultDomain.fail("任务ID不能为空");
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 查询消息
|
||||
TbAiMessage message = messageMapper.selectMessageById(messageId);
|
||||
if (message == null || message.getDeleted()) {
|
||||
resultDomain.fail("消息不存在");
|
||||
if (!StringUtils.hasText(agentId)) {
|
||||
resultDomain.fail("智能体ID不能为空");
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 获取智能体API Key
|
||||
TbAiAgentConfig agent = agentConfigMapper.selectAgentConfigById(message.getAgentID());
|
||||
TbAiAgentConfig agent = agentConfigMapper.selectAgentConfigById(agentId);
|
||||
if (agent == null) {
|
||||
resultDomain.fail("智能体不存在");
|
||||
return resultDomain;
|
||||
@@ -421,21 +506,21 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
|
||||
// 调用Dify停止API
|
||||
TbSysUser currentUser = LoginUtil.getCurrentUser();
|
||||
if (currentUser != null && StringUtils.hasText(message.getDifyMessageId())) {
|
||||
if (currentUser != null) {
|
||||
try {
|
||||
difyApiClient.stopChatMessage(
|
||||
message.getDifyMessageId(),
|
||||
taskId,
|
||||
currentUser.getID(),
|
||||
agent.getDifyApiKey()
|
||||
);
|
||||
log.info("对话停止成功: {}", messageId);
|
||||
log.info("对话停止成功,task_id: {}", taskId);
|
||||
resultDomain.success("停止成功", true);
|
||||
} catch (DifyException e) {
|
||||
log.error("停止对话失败", e);
|
||||
resultDomain.fail("停止失败: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
resultDomain.fail("消息未关联Dify或用户未登录");
|
||||
resultDomain.fail("用户未登录");
|
||||
}
|
||||
|
||||
return resultDomain;
|
||||
@@ -618,8 +703,8 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<TbAiConversation>> listUserConversations(String agentId) {
|
||||
ResultDomain<List<TbAiConversation>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<TbAiConversation> listUserConversations(String agentId) {
|
||||
ResultDomain<TbAiConversation> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
TbSysUser currentUser = LoginUtil.getCurrentUser();
|
||||
@@ -643,8 +728,8 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<TbAiMessage>> listMessages(String conversationId) {
|
||||
ResultDomain<List<TbAiMessage>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<TbAiMessage> listMessages(String conversationId) {
|
||||
ResultDomain<TbAiMessage> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
if (!StringUtils.hasText(conversationId)) {
|
||||
@@ -703,17 +788,17 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResultDomain<TbAiMessage> regenerateAnswer(String messageId, Object callbackObj) {
|
||||
ResultDomain<TbAiMessage> resultDomain = new ResultDomain<>();
|
||||
StreamCallback callback = (callbackObj instanceof StreamCallback) ? (StreamCallback) callbackObj : null;
|
||||
public SseEmitter regenerateAnswerWithSse(String messageId) {
|
||||
// 创建SseEmitter,设置超时时间为5分钟
|
||||
SseEmitter emitter = new SseEmitter(5 * 60 * 1000L);
|
||||
|
||||
try {
|
||||
// 查询原消息
|
||||
TbAiMessage originalMessage = messageMapper.selectMessageById(messageId);
|
||||
if (originalMessage == null || originalMessage.getDeleted()) {
|
||||
resultDomain.fail("消息不存在");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("消息不存在"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
|
||||
// 找到用户的原始问题(上一条消息)
|
||||
@@ -731,32 +816,29 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
}
|
||||
|
||||
if (userQuestion == null) {
|
||||
resultDomain.fail("找不到原始问题");
|
||||
return resultDomain;
|
||||
emitter.send(SseEmitter.event().name("error").data("找不到原始问题"));
|
||||
emitter.complete();
|
||||
return emitter;
|
||||
}
|
||||
|
||||
// 重新发起对话
|
||||
if (callback != null) {
|
||||
return streamChat(
|
||||
originalMessage.getAgentID(),
|
||||
originalMessage.getConversationID(),
|
||||
userQuestion.getContent(),
|
||||
null,
|
||||
callback
|
||||
);
|
||||
} else {
|
||||
return blockingChat(
|
||||
// 直接返回streamChatWithSse的结果
|
||||
return streamChatWithSse(
|
||||
originalMessage.getAgentID(),
|
||||
originalMessage.getConversationID(),
|
||||
userQuestion.getContent(),
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("重新生成回答异常", e);
|
||||
resultDomain.fail("重新生成失败: " + e.getMessage());
|
||||
return resultDomain;
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("error").data(e.getMessage()));
|
||||
emitter.completeWithError(e);
|
||||
} catch (IOException ex) {
|
||||
log.error("发送异常事件失败", ex);
|
||||
emitter.completeWithError(ex);
|
||||
}
|
||||
return emitter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -829,7 +911,49 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 更新评价
|
||||
// 获取智能体配置(需要API Key)
|
||||
TbAiAgentConfig agent = agentConfigMapper.selectAgentConfigById(message.getAgentID());
|
||||
if (agent == null) {
|
||||
resultDomain.fail("智能体不存在");
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 获取当前用户
|
||||
TbSysUser currentUser = LoginUtil.getCurrentUser();
|
||||
if (currentUser == null) {
|
||||
resultDomain.fail("用户未登录");
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 如果有Dify消息ID,同步反馈到Dify
|
||||
if (StringUtils.hasText(message.getDifyMessageId())) {
|
||||
try {
|
||||
// 将评分转换为Dify格式:1=like, -1=dislike, 0=null
|
||||
String difyRating = null;
|
||||
if (rating != null) {
|
||||
if (rating > 0) {
|
||||
difyRating = "like";
|
||||
} else if (rating < 0) {
|
||||
difyRating = "dislike";
|
||||
}
|
||||
// rating == 0 时,difyRating 为 null(取消评价)
|
||||
}
|
||||
|
||||
difyApiClient.submitMessageFeedback(
|
||||
message.getDifyMessageId(),
|
||||
difyRating,
|
||||
currentUser.getID(),
|
||||
feedback,
|
||||
agent.getDifyApiKey()
|
||||
);
|
||||
log.info("Dify消息反馈提交成功: {} - {}", message.getDifyMessageId(), difyRating);
|
||||
} catch (DifyException e) {
|
||||
log.error("提交Dify反馈失败", e);
|
||||
// 不影响本地评价,继续执行
|
||||
}
|
||||
}
|
||||
|
||||
// 更新本地评价
|
||||
TbAiMessage update = new TbAiMessage();
|
||||
update.setID(messageId);
|
||||
update.setRating(rating);
|
||||
|
||||
@@ -354,8 +354,8 @@ public class AiKnowledgeServiceImpl implements AiKnowledgeService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<TbAiKnowledge>> listKnowledges(TbAiKnowledge filter) {
|
||||
ResultDomain<List<TbAiKnowledge>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<TbAiKnowledge> listKnowledges(TbAiKnowledge filter) {
|
||||
ResultDomain<TbAiKnowledge> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
List<UserDeptRoleVO> userDeptRoles = LoginUtil.getCurrentDeptRole();
|
||||
|
||||
@@ -186,12 +186,12 @@ public class AiUploadFileServiceImpl implements AiUploadFileService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResultDomain<List<TbAiUploadFile>> batchUploadToKnowledge(
|
||||
public ResultDomain<TbAiUploadFile> batchUploadToKnowledge(
|
||||
String knowledgeId,
|
||||
List<MultipartFile> files,
|
||||
String indexingTechnique) {
|
||||
|
||||
ResultDomain<List<TbAiUploadFile>> resultDomain = new ResultDomain<>();
|
||||
ResultDomain<TbAiUploadFile> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
if (files == null || files.isEmpty()) {
|
||||
@@ -322,8 +322,8 @@ public class AiUploadFileServiceImpl implements AiUploadFileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<TbAiUploadFile>> listFilesByKnowledge(String knowledgeId) {
|
||||
ResultDomain<List<TbAiUploadFile>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<TbAiUploadFile> listFilesByKnowledge(String knowledgeId) {
|
||||
ResultDomain<TbAiUploadFile> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
if (!StringUtils.hasText(knowledgeId)) {
|
||||
@@ -443,8 +443,8 @@ public class AiUploadFileServiceImpl implements AiUploadFileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<TbAiUploadFile>> syncKnowledgeFiles(String knowledgeId) {
|
||||
ResultDomain<List<TbAiUploadFile>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<TbAiUploadFile> syncKnowledgeFiles(String knowledgeId) {
|
||||
ResultDomain<TbAiUploadFile> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
// 查询知识库的所有文件
|
||||
|
||||
@@ -87,10 +87,7 @@
|
||||
|
||||
<!-- 逻辑删除会话 -->
|
||||
<update id="deleteConversation" parameterType="org.xyzh.common.dto.ai.TbAiConversation">
|
||||
UPDATE tb_ai_conversation
|
||||
SET deleted = 1,
|
||||
delete_time = NOW()
|
||||
WHERE id = #{ID} AND deleted = 0
|
||||
DELETE FROM tb_ai_conversation WHERE id = #{ID}
|
||||
</update>
|
||||
|
||||
<!-- 根据ID查询会话 -->
|
||||
|
||||
@@ -48,14 +48,14 @@ public interface AiAgentConfigService {
|
||||
* 查询所有启用的智能体列表
|
||||
* @return 智能体列表
|
||||
*/
|
||||
ResultDomain<List<TbAiAgentConfig>> listEnabledAgents();
|
||||
ResultDomain<TbAiAgentConfig> listEnabledAgents();
|
||||
|
||||
/**
|
||||
* 查询智能体列表(支持过滤)
|
||||
* @param filter 过滤条件
|
||||
* @return 智能体列表
|
||||
*/
|
||||
ResultDomain<List<TbAiAgentConfig>> listAgents(TbAiAgentConfig filter);
|
||||
ResultDomain<TbAiAgentConfig> listAgents(TbAiAgentConfig filter);
|
||||
|
||||
/**
|
||||
* 分页查询智能体列表
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.xyzh.api.ai.chat;
|
||||
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
import org.xyzh.common.core.domain.ResultDomain;
|
||||
import org.xyzh.common.dto.ai.TbAiConversation;
|
||||
import org.xyzh.common.dto.ai.TbAiMessage;
|
||||
@@ -16,20 +17,18 @@ import java.util.List;
|
||||
public interface AiChatService {
|
||||
|
||||
/**
|
||||
* 流式对话(SSE)
|
||||
* 流式对话(SSE)- 使用SseEmitter实现真正的流式推送
|
||||
* @param agentId 智能体ID
|
||||
* @param conversationId 会话ID(可选,为空则创建新会话)
|
||||
* @param query 用户问题
|
||||
* @param knowledgeIds 使用的知识库ID列表(可选,用于知识库隔离)
|
||||
* @param callback 流式响应回调(StreamCallback类型,需在实现层处理)
|
||||
* @return 对话结果(包含会话ID和消息ID)
|
||||
* @return SseEmitter 流式推送对象
|
||||
*/
|
||||
ResultDomain<TbAiMessage> streamChat(
|
||||
SseEmitter streamChatWithSse(
|
||||
String agentId,
|
||||
String conversationId,
|
||||
String query,
|
||||
List<String> knowledgeIds,
|
||||
Object callback // 使用Object避免跨模块依赖
|
||||
List<String> knowledgeIds
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -47,12 +46,15 @@ public interface AiChatService {
|
||||
List<String> knowledgeIds
|
||||
);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 停止对话生成
|
||||
* @param messageId 消息ID
|
||||
* 停止对话生成(通过Dify TaskID)
|
||||
* @param taskId Dify任务ID
|
||||
* @param agentId 智能体ID
|
||||
* @return 停止结果
|
||||
*/
|
||||
ResultDomain<Boolean> stopChat(String messageId);
|
||||
ResultDomain<Boolean> stopChatByTaskId(String taskId, String agentId);
|
||||
|
||||
/**
|
||||
* 创建新会话
|
||||
@@ -88,14 +90,14 @@ public interface AiChatService {
|
||||
* @param agentId 智能体ID(可选)
|
||||
* @return 会话列表
|
||||
*/
|
||||
ResultDomain<List<TbAiConversation>> listUserConversations(String agentId);
|
||||
ResultDomain<TbAiConversation> listUserConversations(String agentId);
|
||||
|
||||
/**
|
||||
* 查询会话的消息列表
|
||||
* @param conversationId 会话ID
|
||||
* @return 消息列表
|
||||
*/
|
||||
ResultDomain<List<TbAiMessage>> listMessages(String conversationId);
|
||||
ResultDomain<TbAiMessage> listMessages(String conversationId);
|
||||
|
||||
/**
|
||||
* 获取单条消息
|
||||
@@ -105,12 +107,11 @@ public interface AiChatService {
|
||||
ResultDomain<TbAiMessage> getMessage(String messageId);
|
||||
|
||||
/**
|
||||
* 重新生成回答
|
||||
* 重新生成回答(SSE流式)
|
||||
* @param messageId 原消息ID
|
||||
* @param callback 流式回调(可选,StreamCallback类型)
|
||||
* @return 新消息
|
||||
* @return SseEmitter 流式推送对象
|
||||
*/
|
||||
ResultDomain<TbAiMessage> regenerateAnswer(String messageId, Object callback);
|
||||
SseEmitter regenerateAnswerWithSse(String messageId);
|
||||
|
||||
/**
|
||||
* 异步生成会话摘要
|
||||
|
||||
@@ -37,7 +37,7 @@ public interface AiUploadFileService {
|
||||
* @param indexingTechnique 索引方式(可选)
|
||||
* @return 上传结果列表
|
||||
*/
|
||||
ResultDomain<List<TbAiUploadFile>> batchUploadToKnowledge(
|
||||
ResultDomain<TbAiUploadFile> batchUploadToKnowledge(
|
||||
String knowledgeId,
|
||||
List<MultipartFile> files,
|
||||
String indexingTechnique
|
||||
@@ -69,7 +69,7 @@ public interface AiUploadFileService {
|
||||
* @param knowledgeId 知识库ID
|
||||
* @return 文件列表
|
||||
*/
|
||||
ResultDomain<List<TbAiUploadFile>> listFilesByKnowledge(String knowledgeId);
|
||||
ResultDomain<TbAiUploadFile> listFilesByKnowledge(String knowledgeId);
|
||||
|
||||
/**
|
||||
* 分页查询文件列表
|
||||
@@ -91,5 +91,5 @@ public interface AiUploadFileService {
|
||||
* @param knowledgeId 知识库ID
|
||||
* @return 同步结果
|
||||
*/
|
||||
ResultDomain<List<TbAiUploadFile>> syncKnowledgeFiles(String knowledgeId);
|
||||
ResultDomain<TbAiUploadFile> syncKnowledgeFiles(String knowledgeId);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public interface AiKnowledgeService {
|
||||
* @param filter 过滤条件
|
||||
* @return 知识库列表
|
||||
*/
|
||||
ResultDomain<List<TbAiKnowledge>> listKnowledges(TbAiKnowledge filter);
|
||||
ResultDomain<TbAiKnowledge> listKnowledges(TbAiKnowledge filter);
|
||||
|
||||
/**
|
||||
* 分页查询知识库
|
||||
|
||||
@@ -104,16 +104,34 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||
|
||||
/**
|
||||
* @description 从请求中获取Token
|
||||
* 支持以下方式获取Token:
|
||||
* 1. Header中的Authorization: Bearer {token}
|
||||
* 2. URL参数中的token: ?token={token}
|
||||
* 3. URL参数中的access_token: ?access_token={token}
|
||||
* @param request HTTP请求
|
||||
* @return String Token
|
||||
* @author yslg
|
||||
* @since 2025-09-28
|
||||
*/
|
||||
private String getTokenFromRequest(HttpServletRequest request) {
|
||||
// 1. 优先从Header中获取Token
|
||||
String bearerToken = request.getHeader("Authorization");
|
||||
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) {
|
||||
return bearerToken.substring(7);
|
||||
}
|
||||
|
||||
// 2. 从URL参数token中获取
|
||||
String token = request.getParameter("token");
|
||||
if (StringUtils.hasText(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
// 3. 从URL参数access_token中获取
|
||||
String accessToken = request.getParameter("access_token");
|
||||
if (StringUtils.hasText(accessToken)) {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -62,10 +62,10 @@ public class LearningHistoryController {
|
||||
* POST /study/history/list
|
||||
*
|
||||
* @param filter 过滤条件
|
||||
* @return ResultDomain<List<LearningHistoryVO>> 学习历史列表
|
||||
* @return ResultDomain<LearningHistoryVO> 学习历史列表
|
||||
*/
|
||||
@PostMapping("/list")
|
||||
public ResultDomain<List<LearningHistoryVO>> getLearningHistories(@RequestBody TbLearningHistory filter) {
|
||||
public ResultDomain<LearningHistoryVO> getLearningHistories(@RequestBody TbLearningHistory filter) {
|
||||
logger.info("查询学习历史列表,用户ID: {}", filter != null ? filter.getUserID() : null);
|
||||
return learningHistoryService.getLearningHistories(filter);
|
||||
}
|
||||
@@ -103,10 +103,10 @@ public class LearningHistoryController {
|
||||
* POST /study/history/my-histories
|
||||
*
|
||||
* @param filter 过滤条件
|
||||
* @return ResultDomain<List<LearningHistoryVO>> 学习历史列表
|
||||
* @return ResultDomain<LearningHistoryVO> 学习历史列表
|
||||
*/
|
||||
@PostMapping("/my-histories")
|
||||
public ResultDomain<List<LearningHistoryVO>> getCurrentUserLearningHistories(@RequestBody(required = false) TbLearningHistory filter) {
|
||||
public ResultDomain<LearningHistoryVO> getCurrentUserLearningHistories(@RequestBody(required = false) TbLearningHistory filter) {
|
||||
logger.info("查询当前用户学习历史");
|
||||
return learningHistoryService.getCurrentUserLearningHistories(filter);
|
||||
}
|
||||
@@ -116,10 +116,10 @@ public class LearningHistoryController {
|
||||
* GET /study/history/recent
|
||||
*
|
||||
* @param limit 限制数量(可选,默认10)
|
||||
* @return ResultDomain<List<LearningHistoryVO>> 学习历史列表
|
||||
* @return ResultDomain<LearningHistoryVO> 学习历史列表
|
||||
*/
|
||||
@GetMapping("/recent")
|
||||
public ResultDomain<List<LearningHistoryVO>> getRecentLearningHistories(@RequestParam(required = false, defaultValue = "10") Integer limit) {
|
||||
public ResultDomain<LearningHistoryVO> getRecentLearningHistories(@RequestParam(required = false, defaultValue = "10") Integer limit) {
|
||||
logger.info("查询最近学习历史,限制数量: {}", limit);
|
||||
// 从当前登录用户获取
|
||||
return learningHistoryService.getRecentLearningHistories(null, limit);
|
||||
|
||||
@@ -23,11 +23,11 @@ public interface SCLearningHistoryService extends LearningHistoryAPI {
|
||||
/**
|
||||
* @description 查询学习历史列表
|
||||
* @param filter 过滤条件
|
||||
* @return ResultDomain<List<LearningHistoryVO>> 学习历史列表
|
||||
* @return ResultDomain<LearningHistoryVO> 学习历史列表
|
||||
* @author yslg
|
||||
* @since 2025-10-27
|
||||
*/
|
||||
ResultDomain<List<LearningHistoryVO>> getLearningHistories(TbLearningHistory filter);
|
||||
ResultDomain<LearningHistoryVO> getLearningHistories(TbLearningHistory filter);
|
||||
|
||||
/**
|
||||
* @description 分页查询学习历史
|
||||
@@ -71,11 +71,11 @@ public interface SCLearningHistoryService extends LearningHistoryAPI {
|
||||
/**
|
||||
* @description 获取当前用户的学习历史
|
||||
* @param filter 过滤条件
|
||||
* @return ResultDomain<List<LearningHistoryVO>> 学习历史列表
|
||||
* @return ResultDomain<LearningHistoryVO> 学习历史列表
|
||||
* @author yslg
|
||||
* @since 2025-10-27
|
||||
*/
|
||||
ResultDomain<List<LearningHistoryVO>> getCurrentUserLearningHistories(TbLearningHistory filter);
|
||||
ResultDomain<LearningHistoryVO> getCurrentUserLearningHistories(TbLearningHistory filter);
|
||||
|
||||
/**
|
||||
* @description 获取当前用户的学习统计
|
||||
@@ -108,10 +108,10 @@ public interface SCLearningHistoryService extends LearningHistoryAPI {
|
||||
* @description 获取用户最近的学习历史
|
||||
* @param userId 用户ID
|
||||
* @param limit 限制数量
|
||||
* @return ResultDomain<List<LearningHistoryVO>> 学习历史列表
|
||||
* @return ResultDomain<LearningHistoryVO> 学习历史列表
|
||||
* @author yslg
|
||||
* @since 2025-10-27
|
||||
*/
|
||||
ResultDomain<List<LearningHistoryVO>> getRecentLearningHistories(String userId, Integer limit);
|
||||
ResultDomain<LearningHistoryVO> getRecentLearningHistories(String userId, Integer limit);
|
||||
}
|
||||
|
||||
|
||||
@@ -176,8 +176,8 @@ public class SCLearningHistoryServiceImpl implements SCLearningHistoryService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<LearningHistoryVO>> getLearningHistories(TbLearningHistory filter) {
|
||||
ResultDomain<List<LearningHistoryVO>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<LearningHistoryVO> getLearningHistories(TbLearningHistory filter) {
|
||||
ResultDomain<LearningHistoryVO> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
List<LearningHistoryVO> historyVOList = learningHistoryMapper.selectLearningHistoriesWithDetails(filter);
|
||||
@@ -351,8 +351,8 @@ public class SCLearningHistoryServiceImpl implements SCLearningHistoryService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<LearningHistoryVO>> getCurrentUserLearningHistories(TbLearningHistory filter) {
|
||||
ResultDomain<List<LearningHistoryVO>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<LearningHistoryVO> getCurrentUserLearningHistories(TbLearningHistory filter) {
|
||||
ResultDomain<LearningHistoryVO> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
TbSysUser currentUser = LoginUtil.getCurrentUser();
|
||||
@@ -449,8 +449,8 @@ public class SCLearningHistoryServiceImpl implements SCLearningHistoryService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<List<LearningHistoryVO>> getRecentLearningHistories(String userId, Integer limit) {
|
||||
ResultDomain<List<LearningHistoryVO>> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<LearningHistoryVO> getRecentLearningHistories(String userId, Integer limit) {
|
||||
ResultDomain<LearningHistoryVO> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
if (userId == null || userId.isEmpty()) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginElement;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
|
||||
import org.xyzh.common.utils.StringUtils;
|
||||
import org.xyzh.common.utils.spring.SpringContextUtil;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
@@ -78,12 +79,20 @@ public class DatabaseAppender extends AbstractAppender {
|
||||
// 尝试获取请求信息
|
||||
Class<?> servletUtilsClass = Class.forName("org.xyzh.common.utils.ServletUtils");
|
||||
requestUrl = (String) servletUtilsClass.getMethod("getRequestUrl").invoke(null);
|
||||
if(StringUtils.isNotBlank(requestUrl)){
|
||||
// 移除URL参数部分(使用indexOf避免正则表达式问题)
|
||||
int questionMarkIndex = requestUrl.indexOf('?');
|
||||
if(questionMarkIndex != -1) {
|
||||
requestUrl = requestUrl.substring(0, questionMarkIndex);
|
||||
}
|
||||
}
|
||||
requestMethod = (String) servletUtilsClass.getMethod("getHeader", String.class).invoke(null, "X-HTTP-Method-Override");
|
||||
ipAddress = (String) servletUtilsClass.getMethod("getClientIp").invoke(null);
|
||||
ipSource = (String) servletUtilsClass.getMethod("getIpSource").invoke(null);
|
||||
browser = (String) servletUtilsClass.getMethod("getBrowser").invoke(null);
|
||||
os = (String) servletUtilsClass.getMethod("getOs").invoke(null);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
// 非HTTP请求上下文或类不存在,忽略
|
||||
}
|
||||
|
||||
|
||||
@@ -55,8 +55,8 @@ export const aiAgentConfigApi = {
|
||||
* 获取启用的智能体列表
|
||||
* @returns Promise<ResultDomain<AiAgentConfig[]>>
|
||||
*/
|
||||
async listEnabledAgents(): Promise<ResultDomain<AiAgentConfig[]>> {
|
||||
const response = await api.get<AiAgentConfig[]>('/ai/agent/enabled');
|
||||
async listEnabledAgents(): Promise<ResultDomain<AiAgentConfig>> {
|
||||
const response = await api.get<AiAgentConfig>('/ai/agent/enabled');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -76,8 +76,8 @@ export const aiAgentConfigApi = {
|
||||
* @param pageParam 分页参数
|
||||
* @returns Promise<PageDomain<AiAgentConfig>>
|
||||
*/
|
||||
async pageAgents(filter: Partial<AiAgentConfig>, pageParam: PageParam): Promise<PageDomain<AiAgentConfig>> {
|
||||
const response = await api.post<PageDomain<AiAgentConfig>>('/ai/agent/page', {
|
||||
async pageAgents(filter: Partial<AiAgentConfig>, pageParam: PageParam): Promise<ResultDomain<AiAgentConfig>> {
|
||||
const response = await api.post<AiAgentConfig>('/ai/agent/page', {
|
||||
filter,
|
||||
pageParam
|
||||
});
|
||||
@@ -91,7 +91,9 @@ export const aiAgentConfigApi = {
|
||||
* @returns Promise<ResultDomain<boolean>>
|
||||
*/
|
||||
async updateAgentStatus(agentId: string, status: number): Promise<ResultDomain<boolean>> {
|
||||
const response = await api.put<boolean>(`/ai/agent/${agentId}/status`, { status });
|
||||
const response = await api.put<boolean>(`/ai/agent/${agentId}/status`, null, {
|
||||
params: { status }
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -103,9 +105,8 @@ export const aiAgentConfigApi = {
|
||||
* @returns Promise<ResultDomain<boolean>>
|
||||
*/
|
||||
async updateDifyConfig(agentId: string, difyAppId: string, difyApiKey: string): Promise<ResultDomain<boolean>> {
|
||||
const response = await api.put<boolean>(`/ai/agent/${agentId}/dify`, {
|
||||
difyAppId,
|
||||
difyApiKey
|
||||
const response = await api.put<boolean>(`/ai/agent/${agentId}/dify`, null, {
|
||||
params: { difyAppId, difyApiKey }
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -7,12 +7,8 @@
|
||||
import { api } from '@/apis/index';
|
||||
import type {
|
||||
AiConversation,
|
||||
AiMessage,
|
||||
ConversationSearchParams,
|
||||
MessageSearchParams,
|
||||
UserChatStatistics,
|
||||
ConversationStatistics,
|
||||
BatchExportParams,
|
||||
ResultDomain,
|
||||
PageDomain
|
||||
} from '@/types';
|
||||
@@ -27,8 +23,9 @@ export const chatHistoryApi = {
|
||||
* @returns Promise<PageDomain<AiConversation>>
|
||||
*/
|
||||
async pageUserConversations(params: ConversationSearchParams): Promise<PageDomain<AiConversation>> {
|
||||
const response = await api.post<PageDomain<AiConversation>>('/ai/history/conversations/page', params);
|
||||
return response.data;
|
||||
// 后端直接返回PageDomain,不需要再次包装
|
||||
const response = await api.post('/ai/chat/history/conversations/page', params);
|
||||
return response.data as PageDomain<AiConversation>;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -37,18 +34,9 @@ export const chatHistoryApi = {
|
||||
* @returns Promise<PageDomain<AiConversation>>
|
||||
*/
|
||||
async searchConversations(params: MessageSearchParams): Promise<PageDomain<AiConversation>> {
|
||||
const response = await api.post<PageDomain<AiConversation>>('/ai/history/conversations/search', params);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 搜索消息内容(全文搜索)
|
||||
* @param params 搜索参数
|
||||
* @returns Promise<PageDomain<AiMessage>>
|
||||
*/
|
||||
async searchMessages(params: MessageSearchParams): Promise<PageDomain<AiMessage>> {
|
||||
const response = await api.post<PageDomain<AiMessage>>('/ai/history/messages/search', params);
|
||||
return response.data;
|
||||
// 后端直接返回PageDomain,不需要再次包装
|
||||
const response = await api.post('/ai/chat/history/search', params);
|
||||
return response.data as PageDomain<AiConversation>;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -58,7 +46,8 @@ export const chatHistoryApi = {
|
||||
* @returns Promise<ResultDomain<boolean>>
|
||||
*/
|
||||
async toggleFavorite(conversationId: string, isFavorite: boolean): Promise<ResultDomain<boolean>> {
|
||||
const response = await api.put<boolean>(`/ai/history/conversation/${conversationId}/favorite`, {
|
||||
const response = await api.put<boolean>('/ai/chat/history/conversation/favorite', {
|
||||
conversationId,
|
||||
isFavorite
|
||||
});
|
||||
return response.data;
|
||||
@@ -71,7 +60,8 @@ export const chatHistoryApi = {
|
||||
* @returns Promise<ResultDomain<boolean>>
|
||||
*/
|
||||
async togglePin(conversationId: string, isPinned: boolean): Promise<ResultDomain<boolean>> {
|
||||
const response = await api.put<boolean>(`/ai/history/conversation/${conversationId}/pin`, {
|
||||
const response = await api.put<boolean>('/ai/chat/history/conversation/pin', {
|
||||
conversationId,
|
||||
isPinned
|
||||
});
|
||||
return response.data;
|
||||
@@ -83,8 +73,8 @@ export const chatHistoryApi = {
|
||||
* @returns Promise<ResultDomain<number>>
|
||||
*/
|
||||
async batchDeleteConversations(conversationIds: string[]): Promise<ResultDomain<number>> {
|
||||
const response = await api.post<number>('/ai/history/conversations/batch-delete', {
|
||||
conversationIds
|
||||
const response = await api.delete<number>('/ai/chat/history/conversations/batch', {
|
||||
data: { conversationIds }
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
@@ -92,10 +82,10 @@ export const chatHistoryApi = {
|
||||
/**
|
||||
* 获取用户的对话统计信息
|
||||
* @param userId 用户ID(可选,默认当前用户)
|
||||
* @returns Promise<ResultDomain<UserChatStatistics>>
|
||||
* @returns Promise<ResultDomain<any>>
|
||||
*/
|
||||
async getUserChatStatistics(userId?: string): Promise<ResultDomain<UserChatStatistics>> {
|
||||
const response = await api.get<UserChatStatistics>('/ai/history/statistics/user', {
|
||||
async getUserChatStatistics(userId?: string): Promise<ResultDomain<any>> {
|
||||
const response = await api.get<any>('/ai/chat/history/statistics', {
|
||||
params: { userId }
|
||||
});
|
||||
return response.data;
|
||||
@@ -104,10 +94,10 @@ export const chatHistoryApi = {
|
||||
/**
|
||||
* 获取会话的详细统计
|
||||
* @param conversationId 会话ID
|
||||
* @returns Promise<ResultDomain<ConversationStatistics>>
|
||||
* @returns Promise<ResultDomain<any>>
|
||||
*/
|
||||
async getConversationStatistics(conversationId: string): Promise<ResultDomain<ConversationStatistics>> {
|
||||
const response = await api.get<ConversationStatistics>(`/ai/history/statistics/conversation/${conversationId}`);
|
||||
async getConversationStatistics(conversationId: string): Promise<ResultDomain<any>> {
|
||||
const response = await api.get<any>(`/ai/chat/history/conversation/${conversationId}/statistics`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -117,7 +107,7 @@ export const chatHistoryApi = {
|
||||
* @returns Promise<ResultDomain<string>>
|
||||
*/
|
||||
async exportConversationAsMarkdown(conversationId: string): Promise<ResultDomain<string>> {
|
||||
const response = await api.get<string>(`/ai/history/export/markdown/${conversationId}`);
|
||||
const response = await api.get<string>(`/ai/chat/history/export/markdown/${conversationId}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -127,47 +117,7 @@ export const chatHistoryApi = {
|
||||
* @returns Promise<ResultDomain<string>>
|
||||
*/
|
||||
async exportConversationAsJson(conversationId: string): Promise<ResultDomain<string>> {
|
||||
const response = await api.get<string>(`/ai/history/export/json/${conversationId}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 批量导出会话
|
||||
* @param params 导出参数
|
||||
* @returns Promise<ResultDomain<string>>
|
||||
*/
|
||||
async batchExportConversations(params: BatchExportParams): Promise<ResultDomain<string>> {
|
||||
const response = await api.post<string>('/ai/history/export/batch', params);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 下载导出文件
|
||||
* @param conversationId 会话ID
|
||||
* @param format 格式(markdown/json)
|
||||
*/
|
||||
downloadExport(conversationId: string, format: 'markdown' | 'json'): void {
|
||||
const url = `${api.defaults.baseURL}/ai/history/export/download/${conversationId}?format=${format}`;
|
||||
window.open(url, '_blank');
|
||||
},
|
||||
|
||||
/**
|
||||
* 批量下载导出文件
|
||||
* @param conversationIds 会话ID列表
|
||||
* @param format 格式(markdown/json)
|
||||
*/
|
||||
batchDownloadExport(conversationIds: string[], format: 'markdown' | 'json'): void {
|
||||
const url = `${api.defaults.baseURL}/ai/history/export/batch-download?format=${format}&ids=${conversationIds.join(',')}`;
|
||||
window.open(url, '_blank');
|
||||
},
|
||||
|
||||
/**
|
||||
* 清理过期会话(软删除超过N天的会话)
|
||||
* @param days 天数
|
||||
* @returns Promise<ResultDomain<number>>
|
||||
*/
|
||||
async cleanExpiredConversations(days: number): Promise<ResultDomain<number>> {
|
||||
const response = await api.post<number>('/ai/history/clean', { days });
|
||||
const response = await api.get<string>(`/ai/chat/history/export/json/${conversationId}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -176,20 +126,8 @@ export const chatHistoryApi = {
|
||||
* @param limit 数量限制(默认10)
|
||||
* @returns Promise<ResultDomain<AiConversation>>
|
||||
*/
|
||||
async getRecentConversations(limit?: number): Promise<ResultDomain<AiConversation>> {
|
||||
const response = await api.get<AiConversation>('/ai/history/recent', {
|
||||
params: { limit }
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取热门会话(基于消息数或Token数)
|
||||
* @param limit 数量限制(默认10)
|
||||
* @returns Promise<ResultDomain<AiConversation>>
|
||||
*/
|
||||
async getPopularConversations(limit?: number): Promise<ResultDomain<AiConversation>> {
|
||||
const response = await api.get<AiConversation>('/ai/history/popular', {
|
||||
async getRecentConversations(limit = 10): Promise<ResultDomain<AiConversation>> {
|
||||
const response = await api.get<AiConversation>('/ai/chat/history/recent', {
|
||||
params: { limit }
|
||||
});
|
||||
return response.data;
|
||||
|
||||
@@ -9,7 +9,6 @@ import type {
|
||||
AiConversation,
|
||||
AiMessage,
|
||||
ChatRequest,
|
||||
ChatResponse,
|
||||
ResultDomain,
|
||||
StreamCallback
|
||||
} from '@/types';
|
||||
@@ -19,43 +18,102 @@ import type {
|
||||
*/
|
||||
export const chatApi = {
|
||||
/**
|
||||
* 流式对话(SSE)
|
||||
* 流式对话(SSE)- 使用fetch支持Authorization
|
||||
* @param request 对话请求
|
||||
* @param callback 流式回调
|
||||
* @returns Promise<ResultDomain<AiMessage>>
|
||||
*/
|
||||
async streamChat(request: ChatRequest, callback?: StreamCallback): Promise<ResultDomain<AiMessage>> {
|
||||
const token = localStorage.getItem('token');
|
||||
const tokenData = token ? JSON.parse(token) : null;
|
||||
return new Promise((resolve, reject) => {
|
||||
// 使用相对路径走Vite代理,避免跨域
|
||||
const eventSource = new EventSource(
|
||||
`${api.defaults.baseURL}/ai/chat/stream?` +
|
||||
`/api/ai/chat/stream?` +
|
||||
new URLSearchParams({
|
||||
agentId: request.agentId,
|
||||
conversationId: request.conversationId || '',
|
||||
query: request.query,
|
||||
knowledgeIds: request.knowledgeIds?.join(',') || ''
|
||||
knowledgeIds: request.knowledgeIds?.join(',') || '',
|
||||
token: tokenData?.value || ''
|
||||
})
|
||||
);
|
||||
|
||||
let fullMessage = '';
|
||||
// 通知外部EventSource已创建
|
||||
callback?.onStart?.(eventSource);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
let fullMessage = ''; // 累积完整消息内容
|
||||
|
||||
// 监听初始化事件(包含messageId和conversationId)
|
||||
eventSource.addEventListener('init', (event) => {
|
||||
try {
|
||||
const initData = JSON.parse(event.data);
|
||||
console.log('[初始化数据]', initData);
|
||||
// 通知外部保存messageId(用于停止生成)
|
||||
if (callback?.onInit) {
|
||||
callback.onInit(initData);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('解析init事件失败:', event.data);
|
||||
}
|
||||
});
|
||||
|
||||
// 监听标准消息事件
|
||||
eventSource.addEventListener('message', (event) => {
|
||||
const data = event.data;
|
||||
fullMessage += data;
|
||||
callback?.onMessage?.(data);
|
||||
});
|
||||
|
||||
// 监听结束事件
|
||||
eventSource.addEventListener('end', (event) => {
|
||||
const metadata = JSON.parse(event.data);
|
||||
callback?.onMessageEnd?.(metadata);
|
||||
eventSource.close();
|
||||
|
||||
resolve({
|
||||
code: 200,
|
||||
success: true,
|
||||
login: true,
|
||||
auth: true,
|
||||
data: metadata as AiMessage,
|
||||
message: '对话成功'
|
||||
});
|
||||
});
|
||||
|
||||
// 监听所有Dify原始事件(workflow_started, node_started等)
|
||||
const difyEventTypes = [
|
||||
'dify_workflow_started',
|
||||
'dify_node_started',
|
||||
'dify_node_finished',
|
||||
'dify_workflow_finished',
|
||||
'dify_message',
|
||||
'dify_agent_message',
|
||||
'dify_message_end',
|
||||
'dify_message_file',
|
||||
'dify_agent_thought',
|
||||
'dify_ping'
|
||||
];
|
||||
|
||||
difyEventTypes.forEach(eventType => {
|
||||
eventSource.addEventListener(eventType, (event: any) => {
|
||||
try {
|
||||
const eventData = JSON.parse(event.data);
|
||||
console.log(`[Dify事件] ${eventType}:`, eventData);
|
||||
|
||||
// 调用自定义的Dify事件回调
|
||||
if (callback?.onDifyEvent) {
|
||||
const cleanEventType = eventType.replace('dify_', '');
|
||||
callback.onDifyEvent(cleanEventType, eventData);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`解析Dify事件失败 ${eventType}:`, event.data);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 监听错误事件
|
||||
eventSource.addEventListener('error', (event: any) => {
|
||||
const error = new Error(event.data || '对话失败');
|
||||
callback?.onError?.(error);
|
||||
@@ -64,7 +122,7 @@ export const chatApi = {
|
||||
});
|
||||
|
||||
eventSource.onerror = (error) => {
|
||||
callback?.onError?.(error as Error);
|
||||
callback?.onError?.(error as unknown as Error);
|
||||
eventSource.close();
|
||||
reject(error);
|
||||
};
|
||||
@@ -82,7 +140,7 @@ export const chatApi = {
|
||||
},
|
||||
|
||||
/**
|
||||
* 停止对话生成
|
||||
* 停止对话生成(通过消息ID)
|
||||
* @param messageId 消息ID
|
||||
* @returns Promise<ResultDomain<boolean>>
|
||||
*/
|
||||
@@ -91,6 +149,20 @@ export const chatApi = {
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 停止对话生成(通过Dify TaskID)
|
||||
* @param taskId Dify任务ID
|
||||
* @param agentId 智能体ID
|
||||
* @returns Promise<ResultDomain<boolean>>
|
||||
*/
|
||||
async stopChatByTaskId(taskId: string, agentId: string): Promise<ResultDomain<boolean>> {
|
||||
const response = await api.post<boolean>('/ai/chat/stop-by-taskid', {
|
||||
taskId,
|
||||
agentId
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 创建新会话
|
||||
* @param agentId 智能体ID
|
||||
@@ -152,8 +224,8 @@ export const chatApi = {
|
||||
* @param conversationId 会话ID
|
||||
* @returns Promise<ResultDomain<AiMessage[]>>
|
||||
*/
|
||||
async listMessages(conversationId: string): Promise<ResultDomain<AiMessage[]>> {
|
||||
const response = await api.get<AiMessage[]>(`/ai/chat/conversation/${conversationId}/messages`);
|
||||
async listMessages(conversationId: string): Promise<ResultDomain<AiMessage>> {
|
||||
const response = await api.get<AiMessage>(`/ai/chat/conversation/${conversationId}/messages`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -168,46 +240,112 @@ export const chatApi = {
|
||||
},
|
||||
|
||||
/**
|
||||
* 重新生成回答
|
||||
* 重新生成回答(SSE流式)
|
||||
* @param messageId 原消息ID
|
||||
* @param callback 流式回调(可选)
|
||||
* @param callback 流式回调
|
||||
* @returns Promise<ResultDomain<AiMessage>>
|
||||
*/
|
||||
async regenerateAnswer(messageId: string, callback?: StreamCallback): Promise<ResultDomain<AiMessage>> {
|
||||
if (callback) {
|
||||
// 使用流式方式重新生成
|
||||
const token = localStorage.getItem('token');
|
||||
const tokenData = token ? JSON.parse(token) : null;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// 使用相对路径走Vite代理,SSE流式推送
|
||||
const eventSource = new EventSource(
|
||||
`${api.defaults.baseURL}/ai/chat/regenerate/${messageId}?stream=true`
|
||||
`/api/ai/chat/regenerate/${messageId}?` +
|
||||
new URLSearchParams({
|
||||
token: tokenData?.value || ''
|
||||
})
|
||||
);
|
||||
|
||||
eventSource.addEventListener('message', (event) => {
|
||||
callback.onMessage?.(event.data);
|
||||
// 通知外部EventSource已创建
|
||||
callback?.onStart?.(eventSource);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
let fullMessage = ''; // 累积完整消息内容
|
||||
|
||||
// 监听初始化事件(包含messageId和conversationId)
|
||||
eventSource.addEventListener('init', (event) => {
|
||||
try {
|
||||
const initData = JSON.parse(event.data);
|
||||
console.log('[初始化数据-重新生成]', initData);
|
||||
// 通知外部保存messageId(用于停止生成)
|
||||
if (callback?.onInit) {
|
||||
callback.onInit(initData);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('解析init事件失败:', event.data);
|
||||
}
|
||||
});
|
||||
|
||||
// 监听标准消息事件
|
||||
eventSource.addEventListener('message', (event) => {
|
||||
const data = event.data;
|
||||
fullMessage += data;
|
||||
callback?.onMessage?.(data);
|
||||
});
|
||||
|
||||
// 监听结束事件
|
||||
eventSource.addEventListener('end', (event) => {
|
||||
const metadata = JSON.parse(event.data);
|
||||
callback.onMessageEnd?.(metadata);
|
||||
callback?.onMessageEnd?.(metadata);
|
||||
eventSource.close();
|
||||
|
||||
resolve({
|
||||
code: 200,
|
||||
success: true,
|
||||
login: true,
|
||||
auth: true,
|
||||
data: metadata as AiMessage,
|
||||
message: '重新生成成功'
|
||||
});
|
||||
});
|
||||
|
||||
// 监听所有Dify原始事件(workflow_started, node_started等)
|
||||
const difyEventTypes = [
|
||||
'dify_workflow_started',
|
||||
'dify_node_started',
|
||||
'dify_node_finished',
|
||||
'dify_workflow_finished',
|
||||
'dify_message',
|
||||
'dify_agent_message',
|
||||
'dify_message_end',
|
||||
'dify_message_file',
|
||||
'dify_agent_thought',
|
||||
'dify_ping'
|
||||
];
|
||||
|
||||
difyEventTypes.forEach(eventType => {
|
||||
eventSource.addEventListener(eventType, (event: any) => {
|
||||
try {
|
||||
const eventData = JSON.parse(event.data);
|
||||
console.log(`[Dify事件] ${eventType}:`, eventData);
|
||||
|
||||
// 调用自定义的Dify事件回调
|
||||
if (callback?.onDifyEvent) {
|
||||
const cleanEventType = eventType.replace('dify_', '');
|
||||
callback.onDifyEvent(cleanEventType, eventData);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`解析Dify事件失败 ${eventType}:`, event.data);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 监听错误事件
|
||||
eventSource.addEventListener('error', (event: any) => {
|
||||
const error = new Error(event.data || '重新生成失败');
|
||||
callback.onError?.(error);
|
||||
callback?.onError?.(error);
|
||||
eventSource.close();
|
||||
reject(error);
|
||||
});
|
||||
|
||||
eventSource.onerror = (error) => {
|
||||
callback?.onError?.(error as unknown as Error);
|
||||
eventSource.close();
|
||||
reject(error);
|
||||
};
|
||||
});
|
||||
} else {
|
||||
// 使用阻塞方式重新生成
|
||||
const response = await api.post<AiMessage>(`/ai/chat/regenerate/${messageId}`);
|
||||
return response.data;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
*/
|
||||
|
||||
import { api } from '@/apis/index';
|
||||
import type { AiUploadFile, ResultDomain, FileUploadResponse, PageDomain, PageParam } from '@/types';
|
||||
|
||||
import type { AiUploadFile, ResultDomain, FileUploadResponse, PageParam } from '@/types';
|
||||
/**
|
||||
* 文件上传API服务
|
||||
*/
|
||||
@@ -22,11 +21,7 @@ export const fileUploadApi = {
|
||||
formData.append('file', file);
|
||||
formData.append('knowledgeId', knowledgeId);
|
||||
|
||||
const response = await api.post<FileUploadResponse>('/ai/file/upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
});
|
||||
const response = await api.post<FileUploadResponse>('/ai/file/upload', formData);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -43,11 +38,7 @@ export const fileUploadApi = {
|
||||
});
|
||||
formData.append('knowledgeId', knowledgeId);
|
||||
|
||||
const response = await api.post<FileUploadResponse[]>('/ai/file/batch-upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
});
|
||||
const response = await api.post<FileUploadResponse[]>('/ai/file/batch-upload', formData);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -85,10 +76,10 @@ export const fileUploadApi = {
|
||||
* 分页查询文件
|
||||
* @param filter 过滤条件
|
||||
* @param pageParam 分页参数
|
||||
* @returns Promise<PageDomain<AiUploadFile>>
|
||||
* @returns Promise<ResultDomain<AiUploadFile>>
|
||||
*/
|
||||
async pageFiles(filter: Partial<AiUploadFile>, pageParam: PageParam): Promise<PageDomain<AiUploadFile>> {
|
||||
const response = await api.post<PageDomain<AiUploadFile>>('/ai/file/page', {
|
||||
async pageFiles(filter: Partial<AiUploadFile>, pageParam: PageParam): Promise<ResultDomain<AiUploadFile>> {
|
||||
const response = await api.post<AiUploadFile>('/ai/file/page', {
|
||||
filter,
|
||||
pageParam
|
||||
});
|
||||
|
||||
@@ -76,8 +76,8 @@ export const knowledgeApi = {
|
||||
* @param pageParam 分页参数
|
||||
* @returns Promise<PageDomain<AiKnowledge>>
|
||||
*/
|
||||
async pageKnowledges(filter: Partial<AiKnowledge>, pageParam: PageParam): Promise<PageDomain<AiKnowledge>> {
|
||||
const response = await api.post<PageDomain<AiKnowledge>>('/ai/knowledge/page', {
|
||||
async pageKnowledges(filter: Partial<AiKnowledge>, pageParam: PageParam): Promise<ResultDomain<AiKnowledge>> {
|
||||
const response = await api.post<AiKnowledge>('/ai/knowledge/page', {
|
||||
filter,
|
||||
pageParam
|
||||
});
|
||||
@@ -108,6 +108,7 @@ export const knowledgeApi = {
|
||||
* 设置知识库权限
|
||||
* @param params 权限参数
|
||||
* @returns Promise<ResultDomain<boolean>>
|
||||
|
||||
*/
|
||||
async setPermissions(params: KnowledgePermissionParams): Promise<ResultDomain<boolean>> {
|
||||
const response = await api.post<boolean>('/ai/knowledge/permissions', params);
|
||||
@@ -144,3 +145,4 @@ export const knowledgeApi = {
|
||||
return response.data;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -18,26 +18,45 @@ interface CustomAxiosRequestConfig extends Partial<InternalAxiosRequestConfig> {
|
||||
* Token管理
|
||||
*/
|
||||
export const TokenManager = {
|
||||
/** 获取token(优先从localStorage,其次sessionStorage) */
|
||||
/** 获取token(从localStorage获取并检查过期) */
|
||||
getToken(): string | null {
|
||||
return localStorage.getItem('token') || sessionStorage.getItem('token');
|
||||
},
|
||||
const itemStr = localStorage.getItem('token');
|
||||
if (!itemStr) return null;
|
||||
|
||||
/** 设置token(根据rememberMe决定存储位置) */
|
||||
setToken(token: string, rememberMe = false): void {
|
||||
if (rememberMe) {
|
||||
localStorage.setItem('token', token);
|
||||
sessionStorage.removeItem('token'); // 清除sessionStorage中的旧token
|
||||
} else {
|
||||
sessionStorage.setItem('token', token);
|
||||
localStorage.removeItem('token'); // 清除localStorage中的旧token
|
||||
try {
|
||||
const item = JSON.parse(itemStr);
|
||||
const now = Date.now();
|
||||
|
||||
// 检查是否过期
|
||||
if (item.timestamp && item.expiresIn) {
|
||||
if (now - item.timestamp > item.expiresIn) {
|
||||
// 已过期,删除
|
||||
localStorage.removeItem('token');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return item.value || itemStr; // 兼容旧数据
|
||||
} catch {
|
||||
// 如果不是JSON格式,直接返回(兼容旧数据)
|
||||
return itemStr;
|
||||
}
|
||||
},
|
||||
|
||||
/** 移除token(两个存储都清除) */
|
||||
/** 设置token(始终使用localStorage,根据rememberMe设置过期时间) */
|
||||
setToken(token: string, rememberMe = false): void {
|
||||
const data = {
|
||||
value: token,
|
||||
timestamp: Date.now(),
|
||||
// 如果不勾选"记住我",设置1天过期时间;勾选则7天
|
||||
expiresIn: rememberMe ? 7 * 24 * 60 * 60 * 1000 : 1 * 24 * 60 * 60 * 1000
|
||||
};
|
||||
localStorage.setItem('token', JSON.stringify(data));
|
||||
},
|
||||
|
||||
/** 移除token */
|
||||
removeToken(): void {
|
||||
localStorage.removeItem('token');
|
||||
sessionStorage.removeItem('token');
|
||||
},
|
||||
|
||||
/** 检查是否有token */
|
||||
@@ -81,6 +100,11 @@ request.interceptors.request.use(
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// 自动处理 FormData:删除 Content-Type,让浏览器自动设置(包含 boundary)
|
||||
if (config.data instanceof FormData && config.headers) {
|
||||
delete config.headers['Content-Type'];
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
|
||||
|
Before Width: | Height: | Size: 1004 B After Width: | Height: | Size: 1004 B |
@@ -28,6 +28,7 @@
|
||||
|
||||
<!-- 页面内容 -->
|
||||
<main class="main-content">
|
||||
<AIAgent/>
|
||||
<router-view />
|
||||
</main>
|
||||
</div>
|
||||
@@ -35,6 +36,7 @@
|
||||
<!-- 没有侧边栏时直接显示内容 -->
|
||||
<div class="content-wrapper-full" v-else>
|
||||
<main class="main-content-full">
|
||||
<AIAgent/>
|
||||
<router-view />
|
||||
</main>
|
||||
</div>
|
||||
@@ -48,8 +50,9 @@ import { useRoute, useRouter } from 'vue-router';
|
||||
import { useStore } from 'vuex';
|
||||
import type { SysMenu } from '@/types';
|
||||
import { MenuType } from '@/types/enums';
|
||||
import { getMenuPath } from '@/utils/route-generator';
|
||||
import { TopNavigation, MenuSidebar, Breadcrumb } from '@/components';
|
||||
// import { getMenuPath } from '@/utils/route-generator';
|
||||
import { TopNavigation, MenuSidebar } from '@/components';
|
||||
import { AIAgent } from '@/views/public/ai';
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
@@ -89,16 +92,16 @@ const sidebarMenus = computed(() => {
|
||||
// 是否有侧边栏菜单
|
||||
const hasSidebarMenus = computed(() => sidebarMenus.value.length > 0);
|
||||
|
||||
// 面包屑数据
|
||||
const breadcrumbItems = computed(() => {
|
||||
if (!route.meta?.menuId) return [];
|
||||
|
||||
const menuPath = getMenuPath(allMenus.value, route.meta.menuId as string);
|
||||
return menuPath.map((menu) => ({
|
||||
title: menu.name || '',
|
||||
path: menu.url || '',
|
||||
}));
|
||||
});
|
||||
// 面包屑数据(暂时未使用)
|
||||
// const breadcrumbItems = computed(() => {
|
||||
// if (!route.meta?.menuId) return [];
|
||||
//
|
||||
// const menuPath = getMenuPath(allMenus.value, route.meta.menuId as string);
|
||||
// return menuPath.map((menu) => ({
|
||||
// title: menu.name || '',
|
||||
// path: menu.url || '',
|
||||
// }));
|
||||
// });
|
||||
|
||||
// 判断路径是否在菜单下
|
||||
function isPathUnderMenu(path: string, menu: SysMenu): boolean {
|
||||
|
||||
@@ -26,24 +26,45 @@ export interface AuthState {
|
||||
|
||||
// 存储工具函数
|
||||
const StorageUtil = {
|
||||
// 保存数据(根据rememberMe选择存储方式)
|
||||
// 保存数据(始终使用localStorage,但根据rememberMe设置过期时间)
|
||||
setItem(key: string, value: string, rememberMe = false) {
|
||||
if (rememberMe) {
|
||||
localStorage.setItem(key, value);
|
||||
} else {
|
||||
sessionStorage.setItem(key, value);
|
||||
const data = {
|
||||
value,
|
||||
timestamp: Date.now(),
|
||||
// 如果不勾选"记住我",设置1天过期时间;勾选则7天
|
||||
expiresIn: rememberMe ? 7 * 24 * 60 * 60 * 1000 : 1 * 24 * 60 * 60 * 1000
|
||||
};
|
||||
localStorage.setItem(key, JSON.stringify(data));
|
||||
},
|
||||
|
||||
// 获取数据(检查是否过期)
|
||||
getItem(key: string): string | null {
|
||||
const itemStr = localStorage.getItem(key);
|
||||
if (!itemStr) return null;
|
||||
|
||||
try {
|
||||
const item = JSON.parse(itemStr);
|
||||
const now = Date.now();
|
||||
|
||||
// 检查是否过期
|
||||
if (item.timestamp && item.expiresIn) {
|
||||
if (now - item.timestamp > item.expiresIn) {
|
||||
// 已过期,删除
|
||||
localStorage.removeItem(key);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return item.value || itemStr; // 兼容旧数据
|
||||
} catch {
|
||||
// 如果不是JSON格式,直接返回(兼容旧数据)
|
||||
return itemStr;
|
||||
}
|
||||
},
|
||||
|
||||
// 获取数据(优先从localStorage,其次sessionStorage)
|
||||
getItem(key: string): string | null {
|
||||
return localStorage.getItem(key) || sessionStorage.getItem(key);
|
||||
},
|
||||
|
||||
// 删除数据(从两个存储中都删除)
|
||||
// 删除数据
|
||||
removeItem(key: string) {
|
||||
localStorage.removeItem(key);
|
||||
sessionStorage.removeItem(key);
|
||||
},
|
||||
|
||||
// 清除所有认证相关数据
|
||||
@@ -51,7 +72,6 @@ const StorageUtil = {
|
||||
const keys = ['token', 'loginDomain', 'menus', 'permissions', 'rememberMe'];
|
||||
keys.forEach(key => {
|
||||
localStorage.removeItem(key);
|
||||
sessionStorage.removeItem(key);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -321,6 +321,10 @@ export interface KnowledgePermissionParams {
|
||||
* Streaming 回调接口
|
||||
*/
|
||||
export interface StreamCallback {
|
||||
/** 开始连接 */
|
||||
onStart?: (eventSource: EventSource) => void;
|
||||
/** 初始化数据(包含messageId用于停止生成) */
|
||||
onInit?: (initData: { messageId: string; conversationId: string }) => void;
|
||||
/** 接收到消息片段 */
|
||||
onMessage?: (message: string) => void;
|
||||
/** 消息结束 */
|
||||
@@ -329,6 +333,28 @@ export interface StreamCallback {
|
||||
onComplete?: () => void;
|
||||
/** 错误 */
|
||||
onError?: (error: Error) => void;
|
||||
/** Dify原始事件回调(包含task_id等完整信息) */
|
||||
onDifyEvent?: (eventType: string, eventData: any) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dify事件数据接口
|
||||
*/
|
||||
export interface DifyEvent {
|
||||
/** 事件类型 */
|
||||
event: string;
|
||||
/** 任务ID */
|
||||
task_id?: string;
|
||||
/** 工作流运行ID */
|
||||
workflow_run_id?: string;
|
||||
/** 消息ID */
|
||||
message_id?: string;
|
||||
/** 会话ID */
|
||||
conversation_id?: string;
|
||||
/** 创建时间戳 */
|
||||
created_at?: number;
|
||||
/** 其他字段 */
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// ==================== Dify 文档分段相关类型 ====================
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<div class="agent-icon">
|
||||
<img v-if="configForm.avatar" :src="FILE_DOWNLOAD_URL + configForm.avatar" alt="助手头像" />
|
||||
<div v-else class="default-icon">
|
||||
<img src="@/assets/imgs/assisstent.svg" alt="助手头像" />
|
||||
<img src="@/assets/imgs/assistant.svg" alt="助手头像" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="agent-info">
|
||||
@@ -45,34 +45,12 @@
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="模式">
|
||||
<el-select
|
||||
v-model="configForm.modelProvider"
|
||||
placeholder="选择模式"
|
||||
style="width: 100%"
|
||||
>
|
||||
<el-option label="OpenAI" value="openai" />
|
||||
<el-option label="Anthropic" value="anthropic" />
|
||||
<el-option label="Azure OpenAI" value="azure" />
|
||||
<el-option label="通义千问" value="qwen" />
|
||||
<el-option label="文心一言" value="wenxin" />
|
||||
<el-option label="Dify" value="dify" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="模型">
|
||||
<el-input
|
||||
v-model="configForm.modelName"
|
||||
placeholder="例如: gpt-4, claude-3-opus"
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="系统提示词">
|
||||
<el-form-item label="助手描述">
|
||||
<el-input
|
||||
v-model="configForm.systemPrompt"
|
||||
type="textarea"
|
||||
:rows="8"
|
||||
placeholder="请输入系统提示词,定义AI助手的角色、行为和回答风格..."
|
||||
placeholder="请输入助手描述,介绍AI助手的功能、特点和用途..."
|
||||
maxlength="2000"
|
||||
show-word-limit
|
||||
/>
|
||||
@@ -160,10 +138,6 @@ async function handleSave() {
|
||||
ElMessage.warning('请输入助手名称');
|
||||
return;
|
||||
}
|
||||
if (!configForm.value.modelProvider) {
|
||||
ElMessage.warning('请选择模式');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
saving.value = true;
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
<template>
|
||||
<div class="ai-agent" :class="{ expanded: !isBall }">
|
||||
<div v-if="hasAgent" class="ai-agent" :class="{ expanded: !isBall }">
|
||||
<div v-if="isBall" class="ball-container" ref="ballRef">
|
||||
<!-- 悬浮球 -->
|
||||
<div
|
||||
class="chat-ball"
|
||||
@mousedown="startDrag"
|
||||
@touchstart="startDrag"
|
||||
@click="expandChat"
|
||||
:style="ballStyle"
|
||||
>
|
||||
<div class="chat-ball" @mousedown="startDrag" @touchstart="startDrag" :style="ballStyle">
|
||||
<img src="@/assets/imgs/chat-ball.svg" alt="AI助手" class="ball-icon" />
|
||||
<!-- 未读消息提示 -->
|
||||
<div v-if="unreadCount > 0" class="unread-badge">{{ unreadCount }}</div>
|
||||
@@ -27,24 +21,16 @@
|
||||
|
||||
<div v-if="!historyCollapsed" class="history-list">
|
||||
<!-- 新建对话按钮 -->
|
||||
<button class="new-chat-btn" @click="createNewConversation">
|
||||
<button class="new-chat-btn" @click="() => createNewConversation()">
|
||||
+ 新建对话
|
||||
</button>
|
||||
|
||||
<!-- 历史对话列表 -->
|
||||
<div
|
||||
v-for="conv in conversations"
|
||||
:key="conv.id"
|
||||
class="conversation-item"
|
||||
:class="{ active: currentConversation?.id === conv.id }"
|
||||
@click="selectConversation(conv)"
|
||||
>
|
||||
<div v-for="conv in conversations" :key="conv.id" class="conversation-item"
|
||||
:class="{ active: currentConversation?.id === conv.id }" @click="selectConversation(conv)">
|
||||
<div class="conv-title">{{ conv.title || '新对话' }}</div>
|
||||
<div class="conv-time">{{ formatTime(conv.updateTime) }}</div>
|
||||
<button
|
||||
class="delete-conv-btn"
|
||||
@click.stop="deleteConversationConfirm(conv.id || '')"
|
||||
>
|
||||
<button class="delete-conv-btn" @click.stop="deleteConversationConfirm(conv.id || '')">
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
@@ -60,14 +46,8 @@
|
||||
<div class="ai-agent-current-chat">
|
||||
<div class="current-chat-header">
|
||||
<div class="current-chat-title">
|
||||
<input
|
||||
v-if="editingTitle"
|
||||
v-model="editTitleValue"
|
||||
@blur="saveTitle"
|
||||
@keyup.enter="saveTitle"
|
||||
class="title-input"
|
||||
autofocus
|
||||
/>
|
||||
<input v-if="editingTitle" v-model="editTitleValue" @blur="saveTitle" @keyup.enter="saveTitle"
|
||||
class="title-input" autofocus />
|
||||
<span v-else @dblclick="startEditTitle">
|
||||
{{ currentConversation?.title || '新对话' }}
|
||||
</span>
|
||||
@@ -86,9 +66,12 @@
|
||||
<div class="current-chat-content" ref="chatContentRef">
|
||||
<!-- 欢迎消息 -->
|
||||
<div v-if="messages.length === 0" class="welcome-message">
|
||||
<div class="welcome-icon">🤖</div>
|
||||
<h2>你好!我是AI助手</h2>
|
||||
<p>有什么可以帮助你的吗?</p>
|
||||
<div class="welcome-icon">
|
||||
<img v-if="agentAvatarUrl" :src="agentAvatarUrl" alt="AI助手" class="welcome-avatar" />
|
||||
<img v-else src="@/assets/imgs/assistant.svg" alt="AI助手" class="welcome-avatar" />
|
||||
</div>
|
||||
<h2>你好!我是{{ agentConfig?.name || 'AI助手' }}</h2>
|
||||
<p>{{ agentConfig?.systemPrompt || '有什么可以帮助你的吗?' }}</p>
|
||||
</div>
|
||||
|
||||
<!-- 消息列表 -->
|
||||
@@ -107,53 +90,53 @@
|
||||
<!-- AI 消息 -->
|
||||
<div v-else class="message ai-message">
|
||||
<div class="message-avatar">
|
||||
<div class="avatar-circle ai-avatar">🤖</div>
|
||||
<div class="avatar-circle ai-avatar">
|
||||
<img v-if="agentAvatarUrl" :src="agentAvatarUrl" alt="AI助手" class="ai-avatar-img" />
|
||||
<span v-else>🤖</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="message-content">
|
||||
<!-- 如果内容为空且正在生成,显示加载动画 -->
|
||||
<div v-if="!message.content && isGenerating" class="typing-indicator">
|
||||
<span></span>
|
||||
<span></span>
|
||||
<span></span>
|
||||
</div>
|
||||
<!-- 否则显示实际内容 -->
|
||||
<template v-else>
|
||||
<div class="message-text" v-html="formatMarkdown(message.content || '')"></div>
|
||||
<div class="message-footer">
|
||||
<div class="message-time">{{ formatMessageTime(message.createTime) }}</div>
|
||||
<div class="message-actions">
|
||||
<button
|
||||
@click="copyMessage(message.content || '')"
|
||||
class="msg-action-btn"
|
||||
title="复制"
|
||||
>
|
||||
<button @click="copyMessage(message.content || '')" class="msg-action-btn" title="复制">
|
||||
📋
|
||||
</button>
|
||||
<button
|
||||
@click="regenerateMessage(message.id || '')"
|
||||
class="msg-action-btn"
|
||||
title="重新生成"
|
||||
>
|
||||
<button @click="regenerateMessage(message.id || '')" class="msg-action-btn" title="重新生成">
|
||||
🔄
|
||||
</button>
|
||||
<button
|
||||
@click="rateMessage(message.id || '', 1)"
|
||||
class="msg-action-btn"
|
||||
:class="{ active: message.rating === 1 }"
|
||||
title="好评"
|
||||
>
|
||||
<button @click="rateMessage(message.id || '', 1)" class="msg-action-btn"
|
||||
:class="{ active: message.rating === 1 }" title="好评">
|
||||
👍
|
||||
</button>
|
||||
<button
|
||||
@click="rateMessage(message.id || '', -1)"
|
||||
class="msg-action-btn"
|
||||
:class="{ active: message.rating === -1 }"
|
||||
title="差评"
|
||||
>
|
||||
<button @click="rateMessage(message.id || '', -1)" class="msg-action-btn"
|
||||
:class="{ active: message.rating === -1 }" title="差评">
|
||||
👎
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 加载中提示 -->
|
||||
<div v-if="isGenerating" class="message ai-message generating">
|
||||
<!-- 加载中提示:只在还没有AI消息时显示 -->
|
||||
<div v-if="isGenerating && (!messages.length || messages[messages.length - 1]?.role !== 'assistant')"
|
||||
class="message ai-message generating">
|
||||
<div class="message-avatar">
|
||||
<div class="avatar-circle ai-avatar">🤖</div>
|
||||
<div class="avatar-circle ai-avatar">
|
||||
<img v-if="agentAvatarUrl" :src="agentAvatarUrl" alt="AI助手" class="ai-avatar-img" />
|
||||
<img v-else src="@/assets/imgs/assistant.svg" alt="AI助手" class="ai-avatar-img" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="message-content">
|
||||
<div class="typing-indicator">
|
||||
@@ -169,11 +152,7 @@
|
||||
<div class="current-chat-input">
|
||||
<!-- 已上传文件列表 -->
|
||||
<div v-if="uploadedFiles.length > 0" class="input-files">
|
||||
<div
|
||||
v-for="(file, index) in uploadedFiles"
|
||||
:key="index"
|
||||
class="uploaded-file-item"
|
||||
>
|
||||
<div v-for="(file, index) in uploadedFiles" :key="index" class="uploaded-file-item">
|
||||
<span class="file-name">{{ file.name }}</span>
|
||||
<button @click="removeUploadedFile(index)" class="remove-file-btn">×</button>
|
||||
</div>
|
||||
@@ -181,40 +160,28 @@
|
||||
|
||||
<!-- 输入框 -->
|
||||
<div class="input-area">
|
||||
<textarea
|
||||
v-model="inputMessage"
|
||||
class="input-text"
|
||||
placeholder="请输入问题..."
|
||||
@keydown.enter.exact.prevent="sendMessage"
|
||||
@keydown.shift.enter="handleShiftEnter"
|
||||
ref="inputRef"
|
||||
rows="1"
|
||||
/>
|
||||
<textarea v-model="inputMessage" class="input-text" placeholder="请输入问题..."
|
||||
@keydown.enter.exact.prevent="sendMessage" @keydown.shift.enter="handleShiftEnter" ref="inputRef"
|
||||
rows="1" />
|
||||
|
||||
<div class="input-action">
|
||||
<button @click="triggerFileUpload" class="action-icon-btn" title="上传文件">
|
||||
<img src="@/assets/imgs/link.svg" alt="上传文件" class="link-icon" />
|
||||
</button>
|
||||
<button
|
||||
@click="sendMessage"
|
||||
class="action-icon-btn send-btn"
|
||||
:disabled="!canSend"
|
||||
title="发送 (Enter)"
|
||||
>
|
||||
<!-- 停止生成按钮 -->
|
||||
<button v-if="isGenerating" @click="stopGenerating" class="action-icon-btn stop-btn" title="停止生成">
|
||||
<span class="stop-icon">⏹</span>
|
||||
</button>
|
||||
<!-- 发送按钮 -->
|
||||
<button v-else @click="sendMessage" class="action-icon-btn send-btn" :disabled="!canSend" title="发送 (Enter)">
|
||||
<img src="@/assets/imgs/send.svg" alt="发送" class="send-icon" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 隐藏的文件上传input -->
|
||||
<input
|
||||
type="file"
|
||||
ref="fileInputRef"
|
||||
@change="handleFileUpload"
|
||||
style="display: none"
|
||||
multiple
|
||||
accept=".txt,.pdf,.doc,.docx,.md"
|
||||
/>
|
||||
<input type="file" ref="fileInputRef" @change="handleFileUpload" style="display: none" multiple
|
||||
accept=".txt,.pdf,.doc,.docx,.md" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -224,23 +191,84 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue';
|
||||
import { ElMessage, ElMessageBox } from 'element-plus';
|
||||
import { chatApi, chatHistoryApi } from '../../../apis/ai';
|
||||
import type { AiConversation, AiMessage } from '../../../types/ai';
|
||||
import { chatApi, chatHistoryApi, aiAgentConfigApi } from '@/apis/ai';
|
||||
import type { AiConversation, AiMessage, AiAgentConfig } from '@/types/ai';
|
||||
|
||||
interface AIAgentProps {
|
||||
isBall?: boolean;
|
||||
expanded?: boolean;
|
||||
agentId?: string;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<AIAgentProps>(), {
|
||||
isBall: true,
|
||||
expanded: false,
|
||||
agentId: 'default'
|
||||
agentId: undefined
|
||||
});
|
||||
|
||||
// ===== AI助手配置 =====
|
||||
const hasAgent = ref(true);
|
||||
const agentConfig = ref<AiAgentConfig | null>(null);
|
||||
|
||||
// 缓存头像URL为blob,避免重复下载
|
||||
const agentAvatarUrl = ref<string>('');
|
||||
const cachedAvatarPath = ref<string>('');
|
||||
|
||||
// 加载并缓存头像
|
||||
async function loadAndCacheAvatar(avatarPath: string) {
|
||||
if (!avatarPath || cachedAvatarPath.value === avatarPath) {
|
||||
return; // 已缓存,跳过
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/file/download/" + avatarPath);
|
||||
if (response.ok) {
|
||||
const blob = await response.blob();
|
||||
// 释放旧的blob URL
|
||||
if (agentAvatarUrl.value && agentAvatarUrl.value.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(agentAvatarUrl.value);
|
||||
}
|
||||
// 创建新的blob URL
|
||||
agentAvatarUrl.value = URL.createObjectURL(blob);
|
||||
cachedAvatarPath.value = avatarPath;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('加载头像失败:', error);
|
||||
agentAvatarUrl.value = '';
|
||||
}
|
||||
}
|
||||
|
||||
// 加载AI助手配置
|
||||
async function loadAgentConfig() {
|
||||
try {
|
||||
// 优先根据agentId获取,如果没有则获取启用的助手列表
|
||||
if (props.agentId) {
|
||||
const result = await aiAgentConfigApi.getAgentById(props.agentId);
|
||||
if (result.success && result.data) {
|
||||
agentConfig.value = result.data;
|
||||
// 加载并缓存头像
|
||||
if (result.data.avatar) {
|
||||
await loadAndCacheAvatar(result.data.avatar);
|
||||
}
|
||||
} else {
|
||||
hasAgent.value = false;
|
||||
}
|
||||
} else {
|
||||
// 获取启用的助手列表,使用第一个
|
||||
const result = await aiAgentConfigApi.listEnabledAgents();
|
||||
if (result.success && result.dataList && result.dataList.length > 0) {
|
||||
agentConfig.value = result.dataList[0];
|
||||
// 加载并缓存头像
|
||||
if (result.dataList[0].avatar) {
|
||||
await loadAndCacheAvatar(result.dataList[0].avatar);
|
||||
}
|
||||
} else {
|
||||
hasAgent.value = false;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载AI助手配置失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 悬浮球相关 =====
|
||||
const isBall = ref(props.isBall);
|
||||
const isBall = ref(true);
|
||||
const ballRef = ref<HTMLElement | null>(null);
|
||||
const isDragging = ref(false);
|
||||
const dragStartX = ref(0);
|
||||
@@ -249,21 +277,72 @@ const ballX = ref(0);
|
||||
const ballY = ref(0);
|
||||
const unreadCount = ref(0);
|
||||
|
||||
// 存储悬浮球的相对位置(百分比),用于窗口resize时保持相对位置
|
||||
const ballXPercent = ref(1); // 1 表示右侧
|
||||
const ballYPercent = ref(0.5); // 0.5 表示垂直居中
|
||||
const isUserDragged = ref(false); // 标记用户是否手动拖动过
|
||||
|
||||
// 拖拽检测相关
|
||||
const dragStartPosX = ref(0); // 记录拖拽开始时的实际位置
|
||||
const dragStartPosY = ref(0);
|
||||
|
||||
const ballStyle = computed(() => ({
|
||||
left: `${ballX.value}px`,
|
||||
top: `${ballY.value}px`
|
||||
}));
|
||||
|
||||
// 根据百分比计算实际位置
|
||||
function updateBallPosition() {
|
||||
const windowWidth = window.innerWidth;
|
||||
const windowHeight = window.innerHeight;
|
||||
const ballWidth = 40;
|
||||
const ballHeight = 40;
|
||||
const margin = 20;
|
||||
|
||||
// 根据百分比计算位置
|
||||
if (ballXPercent.value < 0.5) {
|
||||
// 左侧
|
||||
ballX.value = margin;
|
||||
} else {
|
||||
// 右侧
|
||||
ballX.value = windowWidth - ballWidth - margin;
|
||||
}
|
||||
|
||||
// 计算Y位置,确保不超出边界
|
||||
let targetY = windowHeight * ballYPercent.value - ballHeight / 2;
|
||||
targetY = Math.max(margin, Math.min(targetY, windowHeight - ballHeight - margin));
|
||||
ballY.value = targetY;
|
||||
}
|
||||
|
||||
// 窗口resize监听器
|
||||
function handleResize() {
|
||||
updateBallPosition();
|
||||
}
|
||||
|
||||
// 初始化悬浮球位置
|
||||
onMounted(() => {
|
||||
// 默认位置:右下角
|
||||
ballX.value = window.innerWidth - 100;
|
||||
ballY.value = window.innerHeight - 100;
|
||||
// 默认位置:右侧,垂直居中(50vh)
|
||||
updateBallPosition();
|
||||
|
||||
// 监听窗口resize事件
|
||||
window.addEventListener('resize', handleResize);
|
||||
|
||||
// 加载AI助手配置
|
||||
loadAgentConfig();
|
||||
|
||||
// 加载最近对话
|
||||
loadRecentConversations();
|
||||
});
|
||||
|
||||
// 清理监听器和资源
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('resize', handleResize);
|
||||
// 释放blob URL
|
||||
if (agentAvatarUrl.value && agentAvatarUrl.value.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(agentAvatarUrl.value);
|
||||
}
|
||||
});
|
||||
|
||||
// 开始拖动
|
||||
function startDrag(e: MouseEvent | TouchEvent) {
|
||||
isDragging.value = true;
|
||||
@@ -274,6 +353,10 @@ function startDrag(e: MouseEvent | TouchEvent) {
|
||||
dragStartX.value = clientX - ballX.value;
|
||||
dragStartY.value = clientY - ballY.value;
|
||||
|
||||
// 记录起始位置,用于判断是点击还是拖拽
|
||||
dragStartPosX.value = ballX.value;
|
||||
dragStartPosY.value = ballY.value;
|
||||
|
||||
document.addEventListener('mousemove', onDrag);
|
||||
document.addEventListener('touchmove', onDrag);
|
||||
document.addEventListener('mouseup', stopDrag);
|
||||
@@ -306,32 +389,53 @@ function stopDrag() {
|
||||
document.removeEventListener('mouseup', stopDrag);
|
||||
document.removeEventListener('touchend', stopDrag);
|
||||
|
||||
// 自动吸附到左右两侧
|
||||
const windowWidth = window.innerWidth;
|
||||
const ballWidth = 80;
|
||||
// 计算移动距离
|
||||
const moveDistanceX = Math.abs(ballX.value - dragStartPosX.value);
|
||||
const moveDistanceY = Math.abs(ballY.value - dragStartPosY.value);
|
||||
const totalDistance = Math.sqrt(moveDistanceX * moveDistanceX + moveDistanceY * moveDistanceY);
|
||||
|
||||
// 判断是点击还是拖拽(移动距离阈值为5px)
|
||||
const isClick = totalDistance <= 5;
|
||||
|
||||
if (isClick) {
|
||||
// 如果是点击,展开对话框
|
||||
expandChat();
|
||||
} else {
|
||||
// 如果是拖拽,执行吸附和位置调整
|
||||
isUserDragged.value = true;
|
||||
|
||||
const windowWidth = window.innerWidth;
|
||||
const windowHeight = window.innerHeight;
|
||||
const ballWidth = 40;
|
||||
const ballHeight = 40;
|
||||
const margin = 20;
|
||||
|
||||
// 自动吸附到左右两侧
|
||||
if (ballX.value < windowWidth / 2) {
|
||||
// 吸附到左侧
|
||||
ballX.value = 20;
|
||||
ballX.value = margin;
|
||||
ballXPercent.value = 0;
|
||||
} else {
|
||||
// 吸附到右侧
|
||||
ballX.value = windowWidth - ballWidth - 20;
|
||||
ballX.value = windowWidth - ballWidth - margin;
|
||||
ballXPercent.value = 1;
|
||||
}
|
||||
|
||||
// 限制垂直位置
|
||||
const windowHeight = window.innerHeight;
|
||||
const ballHeight = 80;
|
||||
// 限制垂直位置并保存百分比
|
||||
if (ballY.value < margin) {
|
||||
ballY.value = margin;
|
||||
} else if (ballY.value > windowHeight - ballHeight - margin) {
|
||||
ballY.value = windowHeight - ballHeight - margin;
|
||||
}
|
||||
|
||||
if (ballY.value < 20) {
|
||||
ballY.value = 20;
|
||||
} else if (ballY.value > windowHeight - ballHeight - 20) {
|
||||
ballY.value = windowHeight - ballHeight - 20;
|
||||
// 保存Y位置的百分比(以中心点计算)
|
||||
ballYPercent.value = (ballY.value + ballHeight / 2) / windowHeight;
|
||||
}
|
||||
}
|
||||
|
||||
// 展开对话
|
||||
function expandChat() {
|
||||
if (isDragging.value) return; // 拖动时不触发
|
||||
if (isDragging.value) return; // 拖动过程中不触发
|
||||
isBall.value = false;
|
||||
}
|
||||
|
||||
@@ -351,8 +455,14 @@ const conversationPage = ref(1);
|
||||
async function loadRecentConversations() {
|
||||
try {
|
||||
const result = await chatHistoryApi.getRecentConversations(10);
|
||||
if (result.success && result.dataList) {
|
||||
conversations.value = result.dataList;
|
||||
if (result.success) {
|
||||
// 后端返回List,所以数据在dataList字段
|
||||
const conversationList = result.dataList || result.data;
|
||||
if (conversationList && Array.isArray(conversationList)) {
|
||||
conversations.value = conversationList;
|
||||
} else {
|
||||
conversations.value = [];
|
||||
}
|
||||
|
||||
// 如果有对话,自动选中第一个
|
||||
if (conversations.value.length > 0 && !currentConversation.value) {
|
||||
@@ -371,15 +481,17 @@ async function loadMoreConversations() {
|
||||
}
|
||||
|
||||
// 新建对话
|
||||
async function createNewConversation() {
|
||||
async function createNewConversation(title?: string) {
|
||||
try {
|
||||
const result = await chatApi.createConversation(props.agentId);
|
||||
const result = await chatApi.createConversation(agentConfig.value!.id!, title);
|
||||
if (result.success && result.data) {
|
||||
currentConversation.value = result.data;
|
||||
conversations.value.unshift(result.data);
|
||||
messages.value = [];
|
||||
if (!title) {
|
||||
ElMessage.success('已创建新对话');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('创建对话失败:', error);
|
||||
ElMessage.error('创建对话失败');
|
||||
@@ -475,6 +587,10 @@ async function saveTitle() {
|
||||
const messages = ref<AiMessage[]>([]);
|
||||
const inputMessage = ref('');
|
||||
const isGenerating = ref(false);
|
||||
const currentEventSource = ref<EventSource | null>(null); // 当前的EventSource连接
|
||||
const currentMessageId = ref<string | null>(null); // 当前AI消息的数据库ID(用于停止生成)
|
||||
const currentTaskId = ref<string | null>(null); // 当前任务的task_id(Dify的消息ID)
|
||||
const difyEventData = ref<Record<string, any>>({}); // 存储Dify事件数据
|
||||
const chatContentRef = ref<HTMLElement | null>(null);
|
||||
const inputRef = ref<HTMLTextAreaElement | null>(null);
|
||||
|
||||
@@ -482,8 +598,10 @@ const inputRef = ref<HTMLTextAreaElement | null>(null);
|
||||
async function loadMessages(conversationId: string) {
|
||||
try {
|
||||
const result = await chatApi.listMessages(conversationId);
|
||||
if (result.success && result.data) {
|
||||
messages.value = result.data;
|
||||
if (result.success) {
|
||||
// 后端返回List,所以数据在dataList字段
|
||||
const messageList = result.dataList || result.data || [];
|
||||
messages.value = Array.isArray(messageList) ? messageList : [];
|
||||
await nextTick();
|
||||
scrollToBottom();
|
||||
}
|
||||
@@ -499,16 +617,19 @@ async function sendMessage() {
|
||||
const message = inputMessage.value.trim();
|
||||
if (!message) return;
|
||||
|
||||
// 如果没有当前对话,创建新对话
|
||||
if (!currentConversation.value) {
|
||||
await createNewConversation();
|
||||
// 如果没有当前对话,创建新对话,使用第一个问题作为标题
|
||||
const isFirstMessage = !currentConversation.value;
|
||||
if (isFirstMessage) {
|
||||
// 限制标题长度为50字符
|
||||
const title = message.length > 50 ? message.substring(0, 50) + '...' : message;
|
||||
await createNewConversation(title);
|
||||
if (!currentConversation.value) return;
|
||||
}
|
||||
|
||||
// 添加用户消息到界面
|
||||
const userMessage: AiMessage = {
|
||||
id: `temp-${Date.now()}`,
|
||||
conversationID: currentConversation.value.id,
|
||||
id: `temp-user-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
|
||||
conversationID: currentConversation.value?.id || '',
|
||||
role: 'user',
|
||||
content: message,
|
||||
createTime: new Date().toISOString(),
|
||||
@@ -527,42 +648,89 @@ async function sendMessage() {
|
||||
try {
|
||||
let aiMessageContent = '';
|
||||
|
||||
await chatApi.streamChat(
|
||||
{
|
||||
agentId: props.agentId,
|
||||
conversationId: currentConversation.value.id,
|
||||
await chatApi.streamChat({
|
||||
agentId: agentConfig.value!.id!,
|
||||
conversationId: currentConversation.value?.id || '',
|
||||
query: message,
|
||||
knowledgeIds: []
|
||||
},
|
||||
{
|
||||
onStart: (eventSource: EventSource) => {
|
||||
// 保存EventSource引用,用于中止
|
||||
currentEventSource.value = eventSource;
|
||||
// 清空之前的数据
|
||||
difyEventData.value = {};
|
||||
currentTaskId.value = null;
|
||||
currentMessageId.value = null;
|
||||
},
|
||||
onInit: (initData: { messageId: string; conversationId: string }) => {
|
||||
// 保存AI消息的数据库ID(task_id),用于停止生成
|
||||
currentMessageId.value = initData.messageId;
|
||||
console.log('[保存MessageID(TaskID)]', initData.messageId);
|
||||
},
|
||||
onMessage: (chunk: string) => {
|
||||
aiMessageContent += chunk;
|
||||
|
||||
// 更新或创建AI消息
|
||||
// 确保AI消息已创建(即使内容为空)
|
||||
const lastMessage = messages.value[messages.value.length - 1];
|
||||
if (lastMessage && lastMessage.role === 'assistant') {
|
||||
lastMessage.content = aiMessageContent;
|
||||
} else {
|
||||
if (!lastMessage || lastMessage.role !== 'assistant') {
|
||||
messages.value.push({
|
||||
id: `temp-ai-${Date.now()}`,
|
||||
id: `temp-ai-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
|
||||
conversationID: currentConversation.value!.id,
|
||||
role: 'assistant',
|
||||
content: aiMessageContent,
|
||||
content: '',
|
||||
createTime: new Date().toISOString(),
|
||||
updateTime: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
|
||||
// 累加内容(包括空chunk,因为后端可能分块发送)
|
||||
if (chunk) {
|
||||
aiMessageContent += chunk;
|
||||
}
|
||||
|
||||
// 更新AI消息内容
|
||||
const aiMessage = messages.value[messages.value.length - 1];
|
||||
if (aiMessage && aiMessage.role === 'assistant') {
|
||||
aiMessage.content = aiMessageContent;
|
||||
}
|
||||
|
||||
nextTick(() => scrollToBottom());
|
||||
},
|
||||
onDifyEvent: (eventType: string, eventData: any) => {
|
||||
// 处理Dify原始事件(包含完整信息)
|
||||
console.log(`[Dify事件] ${eventType}:`, eventData);
|
||||
|
||||
// 存储事件数据
|
||||
difyEventData.value[eventType] = eventData;
|
||||
|
||||
// 特别处理workflow_started事件,提取task_id
|
||||
if (eventType === 'workflow_started' && eventData.task_id) {
|
||||
currentTaskId.value = eventData.task_id;
|
||||
console.log('[Task ID]', eventData.task_id);
|
||||
}
|
||||
|
||||
// 可以根据需要处理其他事件类型
|
||||
// 例如:node_started, node_finished, agent_thought等
|
||||
},
|
||||
onMessageEnd: () => {
|
||||
// 消息结束
|
||||
// 消息结束,如果是第一条消息,更新会话列表中的标题
|
||||
if (isFirstMessage && currentConversation.value) {
|
||||
const convIndex = conversations.value.findIndex(c => c.id === currentConversation.value!.id);
|
||||
if (convIndex !== -1) {
|
||||
conversations.value[convIndex].title = currentConversation.value.title;
|
||||
}
|
||||
}
|
||||
isGenerating.value = false;
|
||||
currentEventSource.value = null;
|
||||
currentTaskId.value = null;
|
||||
currentMessageId.value = null;
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error('对话失败:', error);
|
||||
ElMessage.error('对话失败,请重试');
|
||||
isGenerating.value = false;
|
||||
currentEventSource.value = null;
|
||||
currentTaskId.value = null;
|
||||
currentMessageId.value = null;
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -570,6 +738,7 @@ async function sendMessage() {
|
||||
console.error('发送消息失败:', error);
|
||||
ElMessage.error('发送消息失败');
|
||||
isGenerating.value = false;
|
||||
currentEventSource.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -590,6 +759,53 @@ function scrollToBottom() {
|
||||
}
|
||||
}
|
||||
|
||||
// 停止生成
|
||||
async function stopGenerating() {
|
||||
// 优先使用 taskId,如果没有则使用 messageId
|
||||
const taskIdToStop = currentTaskId.value || currentMessageId.value;
|
||||
|
||||
if (!taskIdToStop || !agentConfig.value?.id) {
|
||||
ElMessage.warning('无法停止:缺少必要信息');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 只调用后端API停止Dify生成,不关闭EventSource
|
||||
// EventSource会在收到Dify的停止/完成事件后自动关闭
|
||||
const result = await chatApi.stopChatByTaskId(taskIdToStop, agentConfig.value.id);
|
||||
|
||||
if (result.success) {
|
||||
ElMessage.success('正在停止生成...');
|
||||
// 注意:不在这里关闭EventSource和清理状态
|
||||
// 等待Dify发送complete/error事件,由onMessageEnd/onError处理
|
||||
} else {
|
||||
ElMessage.warning(result.message || '停止生成失败');
|
||||
// 如果后端返回失败,手动清理状态
|
||||
if (currentEventSource.value) {
|
||||
currentEventSource.value.close();
|
||||
currentEventSource.value = null;
|
||||
}
|
||||
isGenerating.value = false;
|
||||
currentTaskId.value = null;
|
||||
currentMessageId.value = null;
|
||||
difyEventData.value = {};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('停止生成失败:', error);
|
||||
ElMessage.error('停止生成失败');
|
||||
|
||||
// API调用失败时,手动清理状态
|
||||
if (currentEventSource.value) {
|
||||
currentEventSource.value.close();
|
||||
currentEventSource.value = null;
|
||||
}
|
||||
isGenerating.value = false;
|
||||
currentTaskId.value = null;
|
||||
currentMessageId.value = null;
|
||||
difyEventData.value = {};
|
||||
}
|
||||
}
|
||||
|
||||
// 复制消息
|
||||
async function copyMessage(content: string) {
|
||||
try {
|
||||
@@ -604,6 +820,16 @@ async function copyMessage(content: string) {
|
||||
async function regenerateMessage(messageId: string) {
|
||||
if (isGenerating.value) return;
|
||||
|
||||
// 清空原有AI消息内容
|
||||
const messageIndex = messages.value.findIndex(m => m.id === messageId);
|
||||
if (messageIndex === -1) {
|
||||
ElMessage.error('消息不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
// 清空内容,准备重新生成
|
||||
messages.value[messageIndex].content = '';
|
||||
|
||||
isGenerating.value = true;
|
||||
|
||||
try {
|
||||
@@ -612,24 +838,59 @@ async function regenerateMessage(messageId: string) {
|
||||
await chatApi.regenerateAnswer(
|
||||
messageId,
|
||||
{
|
||||
onStart: (eventSource: EventSource) => {
|
||||
// 保存EventSource引用,用于中止
|
||||
currentEventSource.value = eventSource;
|
||||
// 清空之前的数据
|
||||
difyEventData.value = {};
|
||||
currentTaskId.value = null;
|
||||
currentMessageId.value = null;
|
||||
},
|
||||
onInit: (initData: { messageId: string; conversationId: string }) => {
|
||||
// 保存AI消息的数据库ID(task_id),用于停止生成
|
||||
currentMessageId.value = initData.messageId;
|
||||
console.log('[保存MessageID(TaskID)-重新生成]', initData.messageId);
|
||||
},
|
||||
onMessage: (chunk: string) => {
|
||||
// 累加内容(包括空chunk,因为后端可能分块发送)
|
||||
if (chunk) {
|
||||
aiMessageContent += chunk;
|
||||
}
|
||||
|
||||
// 找到对应消息并更新
|
||||
const messageIndex = messages.value.findIndex(m => m.id === messageId);
|
||||
if (messageIndex !== -1) {
|
||||
messages.value[messageIndex].content = aiMessageContent;
|
||||
const msgIndex = messages.value.findIndex(m => m.id === messageId);
|
||||
if (msgIndex !== -1) {
|
||||
messages.value[msgIndex].content = aiMessageContent;
|
||||
}
|
||||
|
||||
nextTick(() => scrollToBottom());
|
||||
},
|
||||
onDifyEvent: (eventType: string, eventData: any) => {
|
||||
// 处理Dify原始事件(包含完整信息)
|
||||
console.log(`[Dify事件-重新生成] ${eventType}:`, eventData);
|
||||
|
||||
// 存储事件数据
|
||||
difyEventData.value[eventType] = eventData;
|
||||
|
||||
// 特别处理workflow_started事件,提取task_id
|
||||
if (eventType === 'workflow_started' && eventData.task_id) {
|
||||
currentTaskId.value = eventData.task_id;
|
||||
console.log('[Task ID-重新生成]', eventData.task_id);
|
||||
}
|
||||
},
|
||||
onMessageEnd: () => {
|
||||
isGenerating.value = false;
|
||||
currentEventSource.value = null;
|
||||
currentTaskId.value = null;
|
||||
currentMessageId.value = null;
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error('重新生成失败:', error);
|
||||
ElMessage.error('重新生成失败');
|
||||
isGenerating.value = false;
|
||||
currentEventSource.value = null;
|
||||
currentTaskId.value = null;
|
||||
currentMessageId.value = null;
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -637,6 +898,7 @@ async function regenerateMessage(messageId: string) {
|
||||
console.error('重新生成失败:', error);
|
||||
ElMessage.error('重新生成失败');
|
||||
isGenerating.value = false;
|
||||
currentEventSource.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -746,16 +1008,14 @@ onUnmounted(() => {
|
||||
<style scoped lang="scss">
|
||||
.ai-agent {
|
||||
position: fixed;
|
||||
z-index: 9999;
|
||||
z-index: 50;
|
||||
|
||||
&.expanded {
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 90vw;
|
||||
max-width: 1200px;
|
||||
height: 80vh;
|
||||
max-height: 800px;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -766,8 +1026,8 @@ onUnmounted(() => {
|
||||
}
|
||||
|
||||
.chat-ball {
|
||||
width: 80px;
|
||||
height: 80px;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
cursor: pointer;
|
||||
position: relative;
|
||||
transition: transform 0.3s ease;
|
||||
@@ -818,7 +1078,7 @@ onUnmounted(() => {
|
||||
|
||||
/* ===== 左侧对话历史 ===== */
|
||||
.ai-agent-history {
|
||||
width: 280px;
|
||||
width: 240px;
|
||||
background: #F9FAFB;
|
||||
border-right: 1px solid #E5E7EB;
|
||||
display: flex;
|
||||
@@ -848,7 +1108,7 @@ onUnmounted(() => {
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
color: #6B7280;
|
||||
color: #6B7240;
|
||||
|
||||
&:hover {
|
||||
color: #E7000B;
|
||||
@@ -918,7 +1178,7 @@ onUnmounted(() => {
|
||||
|
||||
.conv-time {
|
||||
font-size: 12px;
|
||||
color: #6B7280;
|
||||
color: #6B7240;
|
||||
}
|
||||
|
||||
.delete-conv-btn {
|
||||
@@ -958,6 +1218,28 @@ onUnmounted(() => {
|
||||
}
|
||||
}
|
||||
|
||||
.avatar-circle {
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border-radius: 50%;
|
||||
background: #E5E7EB;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 20px;
|
||||
overflow: hidden;
|
||||
|
||||
&.ai-avatar {
|
||||
background: #EFF6FF;
|
||||
}
|
||||
|
||||
.ai-avatar-img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== 右侧当前对话 ===== */
|
||||
.ai-agent-current-chat {
|
||||
flex: 1;
|
||||
@@ -1026,7 +1308,7 @@ onUnmounted(() => {
|
||||
|
||||
span {
|
||||
font-size: 20px;
|
||||
color: #6B7280;
|
||||
color: #6B7240;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1044,6 +1326,16 @@ onUnmounted(() => {
|
||||
.welcome-icon {
|
||||
font-size: 64px;
|
||||
margin-bottom: 16px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
.welcome-avatar {
|
||||
width: 80px;
|
||||
height: 80px;
|
||||
border-radius: 50%;
|
||||
object-fit: cover;
|
||||
}
|
||||
}
|
||||
|
||||
h2 {
|
||||
@@ -1055,7 +1347,7 @@ onUnmounted(() => {
|
||||
|
||||
p {
|
||||
font-size: 16px;
|
||||
color: #6B7280;
|
||||
color: #6B7240;
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
@@ -1066,24 +1358,12 @@ onUnmounted(() => {
|
||||
.message {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: flex-start;
|
||||
|
||||
.message-avatar {
|
||||
flex-shrink: 0;
|
||||
|
||||
.avatar-circle {
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border-radius: 50%;
|
||||
background: #E5E7EB;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 20px;
|
||||
|
||||
&.ai-avatar {
|
||||
background: #EFF6FF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.message-content {
|
||||
@@ -1097,6 +1377,17 @@ onUnmounted(() => {
|
||||
line-height: 1.6;
|
||||
word-wrap: break-word;
|
||||
|
||||
// 图片样式约束
|
||||
:deep(img) {
|
||||
max-width: 100%;
|
||||
max-height: 400px;
|
||||
height: auto;
|
||||
border-radius: 8px;
|
||||
margin: 8px 0;
|
||||
display: block;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
:deep(code) {
|
||||
background: #F3F4F6;
|
||||
padding: 2px 6px;
|
||||
@@ -1163,9 +1454,14 @@ onUnmounted(() => {
|
||||
}
|
||||
}
|
||||
|
||||
// 用户消息靠右(头像在右侧)
|
||||
.user-message {
|
||||
flex-direction: row-reverse;
|
||||
|
||||
.message-content {
|
||||
margin-left: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
|
||||
.message-text {
|
||||
background: #E7000B;
|
||||
@@ -1174,6 +1470,7 @@ onUnmounted(() => {
|
||||
}
|
||||
}
|
||||
|
||||
// AI消息靠左(头像在左侧,默认布局)
|
||||
.ai-message {
|
||||
.message-content {
|
||||
.message-text {
|
||||
@@ -1308,6 +1605,21 @@ onUnmounted(() => {
|
||||
}
|
||||
}
|
||||
|
||||
&.stop-btn {
|
||||
background: #FEF2F2;
|
||||
border-color: #FCA5A5;
|
||||
|
||||
.stop-icon {
|
||||
font-size: 18px;
|
||||
color: #DC2626;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
background: #FEE2E2;
|
||||
border-color: #F87171;
|
||||
}
|
||||
}
|
||||
|
||||
.link-icon,
|
||||
.send-icon {
|
||||
width: 20px;
|
||||
@@ -1320,9 +1632,13 @@ onUnmounted(() => {
|
||||
}
|
||||
|
||||
@keyframes typing {
|
||||
0%, 60%, 100% {
|
||||
|
||||
0%,
|
||||
60%,
|
||||
100% {
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
30% {
|
||||
transform: translateY(-8px);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user