|
|
@ -3,13 +3,16 @@ package cn.teammodel.controller.frontend;
|
|
|
|
import cn.teammodel.common.IdRequest;
|
|
|
|
import cn.teammodel.common.IdRequest;
|
|
|
|
import cn.teammodel.common.R;
|
|
|
|
import cn.teammodel.common.R;
|
|
|
|
import cn.teammodel.model.dto.ai.*;
|
|
|
|
import cn.teammodel.model.dto.ai.*;
|
|
|
|
|
|
|
|
import cn.teammodel.model.entity.TmdUserDetail;
|
|
|
|
import cn.teammodel.model.entity.ai.ChatApp;
|
|
|
|
import cn.teammodel.model.entity.ai.ChatApp;
|
|
|
|
import cn.teammodel.model.entity.ai.ChatSession;
|
|
|
|
import cn.teammodel.model.entity.ai.ChatSession;
|
|
|
|
|
|
|
|
import cn.teammodel.security.utils.SecurityUtil;
|
|
|
|
import cn.teammodel.service.ChatAppService;
|
|
|
|
import cn.teammodel.service.ChatAppService;
|
|
|
|
import cn.teammodel.service.ChatMessageService;
|
|
|
|
import cn.teammodel.service.ChatMessageService;
|
|
|
|
import cn.teammodel.service.ChatSessionService;
|
|
|
|
import cn.teammodel.service.ChatSessionService;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
|
|
|
|
|
@ -33,7 +36,8 @@ public class AiController {
|
|
|
|
@PostMapping("chat/completion")
|
|
|
|
@PostMapping("chat/completion")
|
|
|
|
@ApiOperation("与 spark 的流式对话")
|
|
|
|
@ApiOperation("与 spark 的流式对话")
|
|
|
|
public SseEmitter chatCompletion(@RequestBody @Valid ChatCompletionReqDto chatCompletionReqDto) {
|
|
|
|
public SseEmitter chatCompletion(@RequestBody @Valid ChatCompletionReqDto chatCompletionReqDto) {
|
|
|
|
return chatMessageService.chatCompletion(chatCompletionReqDto);
|
|
|
|
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
|
|
|
|
|
|
|
return chatMessageService.chatCompletion(chatCompletionReqDto, userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @PostMapping("chat/test/completion")
|
|
|
|
// @PostMapping("chat/test/completion")
|
|
|
@ -73,33 +77,40 @@ public class AiController {
|
|
|
|
@GetMapping("session/my")
|
|
|
|
@GetMapping("session/my")
|
|
|
|
@ApiOperation("查询我的聊天会话")
|
|
|
|
@ApiOperation("查询我的聊天会话")
|
|
|
|
public R<List<ChatSession>> listMySession() {
|
|
|
|
public R<List<ChatSession>> listMySession() {
|
|
|
|
List<ChatSession> sessions = chatSessionService.listMySession();
|
|
|
|
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
|
|
|
|
|
|
|
List<ChatSession> sessions = chatSessionService.listMySession(userId);
|
|
|
|
return R.success(sessions);
|
|
|
|
return R.success(sessions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("chat/history/{sessionId}")
|
|
|
|
@GetMapping("chat/history/{sessionId}")
|
|
|
|
@ApiOperation("查询我的聊天记录")
|
|
|
|
@ApiOperation("查询我的聊天记录")
|
|
|
|
public R<List<ChatSession.Message>> getHistory(@PathVariable String sessionId) {
|
|
|
|
public R<List<ChatSession.Message>> getHistory(@PathVariable String sessionId) {
|
|
|
|
List<ChatSession.Message> history = chatSessionService.listMyHistory(sessionId);
|
|
|
|
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
|
|
|
|
|
|
|
List<ChatSession.Message> history = chatSessionService.listHistory(sessionId, userId);
|
|
|
|
return R.success(history);
|
|
|
|
return R.success(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@PostMapping("session/create")
|
|
|
|
@PostMapping("session/create")
|
|
|
|
@ApiOperation("创建聊天会话")
|
|
|
|
@ApiOperation("创建聊天会话")
|
|
|
|
public R<String> createSession() {
|
|
|
|
public R<String> createSession() {
|
|
|
|
String sessionId = chatSessionService.createSession();
|
|
|
|
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
|
|
|
|
|
|
|
String name = (String) ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().get("name");
|
|
|
|
|
|
|
|
name = StringUtils.isBlank(name) ? "老师" : name;
|
|
|
|
|
|
|
|
String sessionId = chatSessionService.createSession(userId, name);
|
|
|
|
return R.success(sessionId);
|
|
|
|
return R.success(sessionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("session/remove")
|
|
|
|
@PostMapping("session/remove")
|
|
|
|
@ApiOperation("删除聊天会话")
|
|
|
|
@ApiOperation("删除聊天会话")
|
|
|
|
public R<String> removeSession(@RequestBody @Valid IdRequest idRequest) {
|
|
|
|
public R<String> removeSession(@RequestBody @Valid IdRequest idRequest) {
|
|
|
|
chatSessionService.deleteSession(idRequest.getId());
|
|
|
|
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
|
|
|
|
|
|
|
chatSessionService.deleteSession(idRequest.getId(), userId);
|
|
|
|
return R.success("删除会话成功");
|
|
|
|
return R.success("删除会话成功");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@PostMapping("session/update")
|
|
|
|
@PostMapping("session/update")
|
|
|
|
@ApiOperation("更新聊天会话")
|
|
|
|
@ApiOperation("更新聊天会话")
|
|
|
|
public R<ChatSession> updateSession(@RequestBody @Valid UpdateSessionDto updateSessionDto) {
|
|
|
|
public R<ChatSession> updateSession(@RequestBody @Valid UpdateSessionDto updateSessionDto) {
|
|
|
|
ChatSession session = chatSessionService.updateSession(updateSessionDto);
|
|
|
|
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
|
|
|
|
|
|
|
|
ChatSession session = chatSessionService.updateSession(updateSessionDto, userId);
|
|
|
|
return R.success(session);
|
|
|
|
return R.success(session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|