Skip to content

Commit 3f61924

Browse files
committed
优化记忆内容
1 parent 765d827 commit 3f61924

File tree

17 files changed

+467
-42
lines changed

17 files changed

+467
-42
lines changed

spring-ai-alibaba-jmanus/pom.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
<maven-checkstyle-plugin.failOnViolation>true</maven-checkstyle-plugin.failOnViolation>
3535
<puppycrawl-tools-checkstyle.version>9.3</puppycrawl-tools-checkstyle.version>
3636

37-
<memory.version>1.0.0.3-SNAPSHOT</memory.version>
38-
3937
</properties>
4038

4139
<dependencyManagement>
@@ -316,13 +314,6 @@
316314
<artifactId>commons-io</artifactId>
317315
<version>2.15.1</version>
318316
</dependency>
319-
320-
<!-- support memory -->
321-
<dependency>
322-
<groupId>com.alibaba.cloud.ai</groupId>
323-
<artifactId>spring-ai-alibaba-starter-memory-jdbc</artifactId>
324-
<version>${memory.version}</version>
325-
</dependency>
326317
</dependencies>
327318

328319

spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/agent/BaseAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private void handleStuckState() {
328328
protected boolean isStuck() {
329329
// Currently, if the agent does not call the tool three times, it is considered
330330
// stuck and the current step is exited.
331-
List<Message> memoryEntries = llmService.getAgentMemory(manusProperties.getMaxMemory()).get(getMemoryId());
331+
List<Message> memoryEntries = llmService.getAgentMemory(manusProperties.getMaxMemory()).get(getCurrentPlanId());
332332
int zeroToolCallCount = 0;
333333
for (Message msg : memoryEntries) {
334334
if (msg instanceof AssistantMessage) {

spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/config/MemoryConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
package com.alibaba.cloud.ai.example.manus.config;
1717

18-
import com.alibaba.cloud.ai.memory.jdbc.H2ChatMemoryRepository;
19-
import com.alibaba.cloud.ai.memory.jdbc.MysqlChatMemoryRepository;
20-
import com.alibaba.cloud.ai.memory.jdbc.PostgresChatMemoryRepository;
18+
import com.alibaba.cloud.ai.example.manus.dynamic.memory.repository.H2ChatMemoryRepository;
19+
import com.alibaba.cloud.ai.example.manus.dynamic.memory.repository.MysqlChatMemoryRepository;
20+
import com.alibaba.cloud.ai.example.manus.dynamic.memory.repository.PostgresChatMemoryRepository;
2121
import org.springframework.ai.chat.memory.ChatMemory;
2222
import org.springframework.ai.chat.memory.ChatMemoryRepository;
2323
import org.springframework.ai.chat.memory.MessageWindowChatMemory;

spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/dynamic/agent/DynamicAgent.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private boolean executeWithRetry(int maxRetries) throws Exception {
170170
List<Message> messages = new ArrayList<>(Collections.singletonList(systemMessage));
171171
// Add history message.
172172
ChatMemory chatMemory = llmService.getAgentMemory(manusProperties.getMaxMemory());
173-
List<Message> historyMem = chatMemory.get(getMemoryId());
173+
List<Message> historyMem = chatMemory.get(getCurrentPlanId());
174174
messages.addAll(historyMem);
175175
messages.add(currentStepEnvMessage);
176176
// Call the LLM
@@ -465,7 +465,7 @@ private void processUserInputToMemory(UserMessage userMessage) {
465465
if (!StringUtils.isBlank(userInput)) {
466466
// Add user input to memory
467467

468-
llmService.getAgentMemory(manusProperties.getMaxMemory()).add(getMemoryId(), userMessage);
468+
llmService.getAgentMemory(manusProperties.getMaxMemory()).add(getCurrentPlanId(), userMessage);
469469

470470
}
471471
}
@@ -481,7 +481,7 @@ private void processMemory(ToolExecutionResult toolExecutionResult) {
481481
return;
482482
}
483483
// clear current plan memory
484-
llmService.getAgentMemory(manusProperties.getMaxMemory()).clear(getMemoryId());
484+
llmService.getAgentMemory(manusProperties.getMaxMemory()).clear(getCurrentPlanId());
485485
for (Message message : messages) {
486486
// exclude all system message
487487
if (message instanceof SystemMessage) {
@@ -493,7 +493,7 @@ private void processMemory(ToolExecutionResult toolExecutionResult) {
493493
continue;
494494
}
495495
// only keep assistant message and tool_call message
496-
llmService.getAgentMemory(manusProperties.getMaxMemory()).add(getMemoryId(), message);
496+
llmService.getAgentMemory(manusProperties.getMaxMemory()).add(getCurrentPlanId(), message);
497497
}
498498
}
499499

spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/dynamic/memory/advisor/CustomMessageChatMemoryAdvisor.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public class CustomMessageChatMemoryAdvisor implements BaseChatMemoryAdvisor {
4545

4646
private String userRequest;
4747

48+
private AdvisorType advisorType;
49+
4850
private CustomMessageChatMemoryAdvisor(ChatMemory chatMemory, String defaultConversationId, int order,
49-
Scheduler scheduler, String userRequest) {
51+
Scheduler scheduler, String userRequest, AdvisorType advisorType) {
5052
Assert.notNull(chatMemory, "chatMemory cannot be null");
5153
Assert.hasText(defaultConversationId, "defaultConversationId cannot be null or empty");
5254
Assert.notNull(scheduler, "scheduler cannot be null");
@@ -55,6 +57,7 @@ private CustomMessageChatMemoryAdvisor(ChatMemory chatMemory, String defaultConv
5557
this.order = order;
5658
this.scheduler = scheduler;
5759
this.userRequest = userRequest;
60+
this.advisorType = advisorType;
5861
}
5962

6063
@Override
@@ -83,6 +86,10 @@ public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChai
8386
.prompt(chatClientRequest.prompt().mutate().messages(processedMessages).build())
8487
.build();
8588

89+
if (this.advisorType == AdvisorType.AFTER) {
90+
return processedChatClientRequest;
91+
}
92+
8693
// 4. Add the new user message to the conversation memory.
8794
UserMessage userMessage = processedChatClientRequest.prompt().getUserMessage();
8895

@@ -104,6 +111,9 @@ public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChai
104111

105112
@Override
106113
public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorChain advisorChain) {
114+
if (this.advisorType == AdvisorType.BEFORE) {
115+
return chatClientResponse;
116+
}
107117
List<Message> assistantMessages = new ArrayList<>();
108118
if (chatClientResponse.chatResponse() != null) {
109119
assistantMessages = chatClientResponse.chatResponse()
@@ -132,8 +142,9 @@ public Flux<ChatClientResponse> adviseStream(ChatClientRequest chatClientRequest
132142
response -> this.after(response, streamAdvisorChain)));
133143
}
134144

135-
public static CustomMessageChatMemoryAdvisor.Builder builder(ChatMemory chatMemory, String userRequest) {
136-
return new CustomMessageChatMemoryAdvisor.Builder(chatMemory, userRequest);
145+
public static CustomMessageChatMemoryAdvisor.Builder builder(ChatMemory chatMemory, String userRequest,
146+
AdvisorType advisorType) {
147+
return new CustomMessageChatMemoryAdvisor.Builder(chatMemory, userRequest, advisorType);
137148
}
138149

139150
public static final class Builder {
@@ -148,9 +159,12 @@ public static final class Builder {
148159

149160
private String userRequest;
150161

151-
private Builder(ChatMemory chatMemory, String userRequest) {
162+
private AdvisorType advisorType;
163+
164+
private Builder(ChatMemory chatMemory, String userRequest, AdvisorType advisorType) {
152165
this.chatMemory = chatMemory;
153166
this.userRequest = userRequest;
167+
this.advisorType = advisorType;
154168
}
155169

156170
/**
@@ -184,9 +198,15 @@ public CustomMessageChatMemoryAdvisor.Builder scheduler(Scheduler scheduler) {
184198
*/
185199
public CustomMessageChatMemoryAdvisor build() {
186200
return new CustomMessageChatMemoryAdvisor(this.chatMemory, this.conversationId, this.order, this.scheduler,
187-
this.userRequest);
201+
this.userRequest, this.advisorType);
188202
}
189203

190204
}
191205

206+
public enum AdvisorType {
207+
208+
BEFORE, AFTER, ALL
209+
210+
}
211+
192212
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.cloud.ai.example.manus.dynamic.memory.repository;
17+
18+
import org.springframework.jdbc.core.JdbcTemplate;
19+
20+
/**
21+
* auth: dahua
22+
*/
23+
public class H2ChatMemoryRepository extends JdbcChatMemoryRepository {
24+
25+
// H2 specific query statements
26+
private static final String H2_QUERY_ADD = "INSERT INTO ai_chat_memory (conversation_id, content, type, timestamp) VALUES (?, ?, ?, ?)";
27+
28+
private static final String H2_QUERY_GET = "SELECT content, type FROM ai_chat_memory WHERE conversation_id = ? ORDER BY timestamp";
29+
30+
private H2ChatMemoryRepository(JdbcTemplate jdbcTemplate) {
31+
super(jdbcTemplate);
32+
}
33+
34+
public static H2Builder h2Builder() {
35+
return new H2Builder();
36+
}
37+
38+
public static class H2Builder {
39+
40+
private JdbcTemplate jdbcTemplate;
41+
42+
public H2Builder jdbcTemplate(JdbcTemplate jdbcTemplate) {
43+
this.jdbcTemplate = jdbcTemplate;
44+
return this;
45+
}
46+
47+
public H2ChatMemoryRepository build() {
48+
return new H2ChatMemoryRepository(this.jdbcTemplate);
49+
}
50+
51+
}
52+
53+
@Override
54+
protected String hasTableSql(String tableName) {
55+
return String.format("SELECT table_name FROM information_schema.tables WHERE table_name = '%s'", tableName);
56+
}
57+
58+
@Override
59+
protected String createTableSql(String tableName) {
60+
return String.format(
61+
"CREATE TABLE %s (id BIGINT AUTO_INCREMENT PRIMARY KEY, "
62+
+ "conversation_id VARCHAR(256) NOT NULL, content LONGTEXT NOT NULL, "
63+
+ "type VARCHAR(100) NOT NULL, timestamp TIMESTAMP NOT NULL, "
64+
+ "CONSTRAINT chk_message_type CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL')))",
65+
tableName);
66+
}
67+
68+
@Override
69+
protected String getAddSql() {
70+
return H2_QUERY_ADD;
71+
}
72+
73+
@Override
74+
protected String getGetSql() {
75+
return H2_QUERY_GET;
76+
}
77+
78+
}

0 commit comments

Comments
 (0)