Skip to content

Commit 6bf07a1

Browse files
author
Yuriy Bezsonov
committed
refactoring
1 parent a310c66 commit 6bf07a1

8 files changed

Lines changed: 21 additions & 166 deletions

File tree

samples/spring-ai-te-agent/ai-agent/samples/expense_policy.md renamed to samples/spring-ai-te-agent/ai-agent/samples/policy-expense.md

File renamed without changes.

samples/spring-ai-te-agent/ai-agent/samples/travel_policy.md renamed to samples/spring-ai-te-agent/ai-agent/samples/policy-travel.md

File renamed without changes.

samples/spring-ai-te-agent/ai-agent/samples/travel_and_expenses_policy.md

Lines changed: 0 additions & 138 deletions
This file was deleted.

samples/spring-ai-te-agent/ai-agent/src/main/java/com/example/ai/agent/controller/ChatController.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ public class ChatController {
2020
private static final Logger logger = LoggerFactory.getLogger(ChatController.class);
2121

2222
private final ChatService chatService;
23-
private final DocumentChatService documentChatService;
24-
private final ConversationSummaryService summaryService;
2523
private final ChatMemoryService chatMemoryService;
24+
private final ConversationSummaryService summaryService;
25+
private final DocumentChatService documentChatService;
2626

27-
public ChatController(ChatService chatService,
28-
DocumentChatService documentChatService,
27+
public ChatController(ChatMemoryService chatMemoryService,
2928
ConversationSummaryService summaryService,
30-
ChatMemoryService chatMemoryService) {
29+
DocumentChatService documentChatService,
30+
ChatService chatService) {
3131
this.chatService = chatService;
32-
this.documentChatService = documentChatService;
33-
this.summaryService = summaryService;
3432
this.chatMemoryService = chatMemoryService;
33+
this.summaryService = summaryService;
34+
this.documentChatService = documentChatService;
3535
}
3636

3737
@PostMapping(value = "message", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)

samples/spring-ai-te-agent/ai-agent/src/main/java/com/example/ai/agent/controller/WebViewController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.example.ai.agent.controller;
22

3-
import org.springframework.beans.factory.annotation.Value;
43
import org.springframework.stereotype.Controller;
5-
import org.springframework.ui.Model;
64
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.ui.Model;
6+
import org.springframework.beans.factory.annotation.Value;
77

88
@Controller
99
public class WebViewController {

samples/spring-ai-te-agent/ai-agent/src/main/java/com/example/ai/agent/service/ChatMemoryService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ public class ChatMemoryService {
2828
private final MessageWindowChatMemory sessionMemory;
2929
private final MessageWindowChatMemory contextMemory;
3030
private final MessageWindowChatMemory preferencesMemory;
31-
private final ChatResponseExtractor responseExtractor;
3231

3332
// Thread-local to store current userId per request
3433
private final ThreadLocal<String> currentUserId = ThreadLocal.withInitial(() -> "user1");
3534

36-
public ChatMemoryService(DataSource dataSource, ChatResponseExtractor responseExtractor) {
37-
this.responseExtractor = responseExtractor;
35+
public ChatMemoryService(DataSource dataSource) {
3836

3937
var jdbcRepository = JdbcChatMemoryRepository.builder()
4038
.dataSource(dataSource)
@@ -145,7 +143,9 @@ private void loadPreviousContext(String conversationId, ChatClient chatClient) {
145143
.call()
146144
.chatResponse();
147145

148-
String contextSummary = responseExtractor.extractText(chatResponse);
146+
String contextSummary = (chatResponse != null && chatResponse.getResult() != null && chatResponse.getResult().getOutput() != null)
147+
? chatResponse.getResult().getOutput().getText()
148+
: null;
149149

150150
if (contextSummary == null || contextSummary.isEmpty()) {
151151
logger.warn("Failed to generate context summary");

samples/spring-ai-te-agent/ai-agent/src/main/java/com/example/ai/agent/service/ChatService.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class ChatService {
1818

1919
private final ChatClient chatClient;
2020
private final ChatMemoryService chatMemoryService;
21-
private final ChatResponseExtractor responseExtractor;
2221

2322
public static final String SYSTEM_PROMPT = """
2423
You are a helpful AI Agent for travel and expenses.
@@ -30,14 +29,13 @@ public class ChatService {
3029
4. Use tools for dynamic data (flights, weather, bookings, currency)
3130
""";
3231

33-
public ChatService(ChatResponseExtractor responseExtractor,
34-
ChatMemoryService chatMemoryService,
32+
public ChatService(ChatMemoryService chatMemoryService,
3533
VectorStore vectorStore,
3634
DateTimeService dateTimeService,
3735
WeatherService weatherService,
3836
ToolCallbackProvider tools,
3937
ChatClient.Builder chatClientBuilder) {
40-
this.responseExtractor = responseExtractor;
38+
4139
this.chatClient = chatClientBuilder
4240
.defaultSystem(SYSTEM_PROMPT)
4341
.defaultAdvisors(
@@ -64,8 +62,4 @@ public Flux<String> processChat(String prompt) {
6462
return Flux.just("I don't know - there was an error processing your request.");
6563
}
6664
}
67-
68-
public String extractTextFromResponse(org.springframework.ai.chat.model.ChatResponse chatResponse) {
69-
return responseExtractor.extractText(chatResponse);
70-
}
7165
}

samples/spring-ai-te-agent/ai-agent/src/main/java/com/example/ai/agent/service/ConversationSummaryService.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ public class ConversationSummaryService {
1414
private static final Logger logger = LoggerFactory.getLogger(ConversationSummaryService.class);
1515

1616
private final ChatMemoryService chatMemoryService;
17-
private final ChatResponseExtractor responseExtractor;
1817
private final ChatClient chatClient;
1918

2019
public ConversationSummaryService(ChatMemoryService chatMemoryService,
21-
ChatResponseExtractor responseExtractor,
2220
ChatClient.Builder chatClientBuilder) {
2321
this.chatMemoryService = chatMemoryService;
24-
this.responseExtractor = responseExtractor;
2522
this.chatClient = chatClientBuilder.build();
2623
}
2724

@@ -56,7 +53,7 @@ public String summarizeAndSave(String conversationId) {
5653

5754
String preferencesPrompt = existingPrefsText.isEmpty()
5855
? "Extract static user information (name, email, preferences). If none, return empty."
59-
: "Merge preferences. Keep existing:\n" + existingPrefsText +
56+
: "Merge preferences. Keep existing:\n" + existingPrefsText +
6057
"\nAdd new from conversation. Update only if explicitly changed. Never remove existing.";
6158

6259
var chatResponse = chatClient.prompt()
@@ -79,7 +76,9 @@ public String summarizeAndSave(String conversationId) {
7976
.call()
8077
.chatResponse();
8178

82-
String response = responseExtractor.extractText(chatResponse);
79+
String response = (chatResponse != null && chatResponse.getResult() != null && chatResponse.getResult().getOutput() != null)
80+
? chatResponse.getResult().getOutput().getText()
81+
: null;
8382

8483
if (response == null || response.trim().isEmpty()) {
8584
logger.error("Generated summary is null or empty");
@@ -131,9 +130,9 @@ public String summarizeAndSave(String conversationId) {
131130

132131
String timestamp = java.time.LocalDateTime.now()
133132
.format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd HHmmss"));
134-
133+
135134
return "**Summary:** [" + timestamp + "]\n\n" +
136-
"**Preferences:**\n" + (preferences.isEmpty() ? "None" : preferences) +
135+
"**Preferences:**\n" + (preferences.isEmpty() ? "None" : preferences) +
137136
"\n\n**Context:**\n" + context;
138137
}
139138
}

0 commit comments

Comments
 (0)