File size: 4,554 Bytes
5a44b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.example.p1.controller;

import com.example.p1.service.ChatService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/chat")
@CrossOrigin(origins = {"http://localhost:3000", "http://localhost:8080"})
public class ChatController {

    private final ChatService chatService;

    public ChatController(ChatService chatService) {
        this.chatService = chatService;
    }

    @PostMapping("/message")
    public Mono<ResponseEntity<ChatResponse>> sendMessage(@Valid @RequestBody ChatRequest request) {
        return chatService.generateResponse(request.getMessage())
                .map(response -> ResponseEntity.ok(new ChatResponse(response, true, null)))
                .onErrorReturn(ResponseEntity.badRequest()
                        .body(new ChatResponse(null, false, "Failed to generate response")));
    }

    @PostMapping("/conversation")
    public Mono<ResponseEntity<ChatResponse>> sendConversation(@Valid @RequestBody ConversationRequest request) {
        return chatService.generateConversationResponse(request.getMessages())
                .map(response -> ResponseEntity.ok(new ChatResponse(response, true, null)))
                .onErrorReturn(ResponseEntity.badRequest()
                        .body(new ChatResponse(null, false, "Failed to generate response")));
    }

    @PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> streamMessage(@Valid @RequestBody ChatRequest request) {
        return chatService.generateStreamingResponse(request.getMessage());
    }

    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> health() {
        return ResponseEntity.ok(Map.of(
                "status", "UP",
                "service", "Chat Service",
                "timestamp", System.currentTimeMillis()
        ));
    }

    // DTOs
    public static class ChatRequest {
        @NotBlank(message = "Message cannot be blank")
        private String message;

        public ChatRequest() {}

        public ChatRequest(String message) {
            this.message = message;
        }

        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
    }

    public static class ConversationRequest {
        @Valid
        private List<Message> messages;

        public ConversationRequest() {}

        public ConversationRequest(List<Message> messages) {
            this.messages = messages;
        }

        public List<Message> getMessages() { return messages; }
        public void setMessages(List<Message> messages) { this.messages = messages; }

        public static class Message {
            @NotBlank
            private String role; // "user" or "assistant"
            @NotBlank
            private String content;

            public Message() {}

            public Message(String role, String content) {
                this.role = role;
                this.content = content;
            }

            public String getRole() { return role; }
            public void setRole(String role) { this.role = role; }

            public String getContent() { return content; }
            public void setContent(String content) { this.content = content; }
        }
    }

    public static class ChatResponse {
        private String response;
        private boolean success;
        private String error;
        private long timestamp;

        public ChatResponse(String response, boolean success, String error) {
            this.response = response;
            this.success = success;
            this.error = error;
            this.timestamp = System.currentTimeMillis();
        }

        public String getResponse() { return response; }
        public void setResponse(String response) { this.response = response; }

        public boolean isSuccess() { return success; }
        public void setSuccess(boolean success) { this.success = success; }

        public String getError() { return error; }
        public void setError(String error) { this.error = error; }

        public long getTimestamp() { return timestamp; }
        public void setTimestamp(long timestamp) { this.timestamp = timestamp; }
    }
}