zhou zhou
16 小时以前 1d0ab9996661fdc66037870d4b98037f2dfa079a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.vincent.rsf.server.ai.controller;
 
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.server.ai.dto.AiChatSessionPinRequest;
import com.vincent.rsf.server.ai.dto.AiChatSessionRenameRequest;
import com.vincent.rsf.server.ai.dto.AiChatRequest;
import com.vincent.rsf.server.ai.service.AiChatService;
import com.vincent.rsf.server.system.controller.BaseController;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 
import java.util.UUID;
 
@RestController
@Slf4j
@RequiredArgsConstructor
public class AiChatController extends BaseController {
 
    private final AiChatService aiChatService;
 
    @PreAuthorize("isAuthenticated()")
    @GetMapping("/ai/chat/runtime")
    public R runtime(@RequestParam(required = false) String promptCode,
                     @RequestParam(required = false) Long sessionId) {
        return R.ok().add(aiChatService.getRuntime(promptCode, sessionId, getLoginUserId(), getTenantId()));
    }
 
    @PreAuthorize("isAuthenticated()")
    @GetMapping("/ai/chat/sessions")
    public R sessions(@RequestParam(required = false) String promptCode,
                      @RequestParam(required = false) String keyword) {
        return R.ok().add(aiChatService.listSessions(promptCode, keyword, getLoginUserId(), getTenantId()));
    }
 
    @PreAuthorize("isAuthenticated()")
    @PostMapping("/ai/chat/session/remove/{sessionId}")
    public R removeSession(@PathVariable Long sessionId) {
        aiChatService.removeSession(sessionId, getLoginUserId(), getTenantId());
        return R.ok("Delete Success").add(sessionId);
    }
 
    @PreAuthorize("isAuthenticated()")
    @PostMapping("/ai/chat/session/rename/{sessionId}")
    public R renameSession(@PathVariable Long sessionId, @RequestBody AiChatSessionRenameRequest request) {
        return R.ok("Update Success").add(aiChatService.renameSession(sessionId, request, getLoginUserId(), getTenantId()));
    }
 
    @PreAuthorize("isAuthenticated()")
    @PostMapping("/ai/chat/session/pin/{sessionId}")
    public R pinSession(@PathVariable Long sessionId, @RequestBody AiChatSessionPinRequest request) {
        return R.ok("Update Success").add(aiChatService.pinSession(sessionId, request, getLoginUserId(), getTenantId()));
    }
 
    @PreAuthorize("isAuthenticated()")
    @PostMapping("/ai/chat/session/memory/clear/{sessionId}")
    public R clearSessionMemory(@PathVariable Long sessionId) {
        aiChatService.clearSessionMemory(sessionId, getLoginUserId(), getTenantId());
        return R.ok("Clear Success").add(sessionId);
    }
 
    @PreAuthorize("isAuthenticated()")
    @PostMapping("/ai/chat/session/memory/retain-latest/{sessionId}")
    public R retainLatestRound(@PathVariable Long sessionId) {
        aiChatService.retainLatestRound(sessionId, getLoginUserId(), getTenantId());
        return R.ok("Retain Success").add(sessionId);
    }
 
    @PreAuthorize("isAuthenticated()")
    @PostMapping(value = "/ai/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter stream(@RequestBody AiChatRequest request) {
        String requestId = StringUtils.hasText(request.getRequestId())
                ? request.getRequestId().trim()
                : UUID.randomUUID().toString().replace("-", "");
        request.setRequestId(requestId);
        log.info("AI chat request accepted, requestId={}, userId={}, tenantId={}, sessionId={}",
                requestId, getLoginUserId(), getTenantId(), request.getSessionId());
        return aiChatService.stream(request, getLoginUserId(), getTenantId());
    }
}