Skip to content

Commit 7ed9881

Browse files
authored
fix(jmanus): optimize the incompatibility issues of postgres database (alibaba#2082)
* 优化pg数据库不兼容问题,也为更多数据库兼容做铺垫 * 去除无效引用 * 添加协议
1 parent faa1537 commit 7ed9881

File tree

6 files changed

+108
-28
lines changed

6 files changed

+108
-28
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 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.dialect;
17+
18+
import com.alibaba.cloud.ai.example.manus.recorder.entity.PlanExecutionRecordEntity;
19+
import org.checkerframework.checker.initialization.qual.Initialized;
20+
import org.checkerframework.checker.nullness.qual.NonNull;
21+
import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
22+
import org.hibernate.boot.Metadata;
23+
import org.hibernate.dialect.Dialect;
24+
import org.hibernate.dialect.H2Dialect;
25+
import org.hibernate.dialect.MySQLDialect;
26+
import org.hibernate.dialect.PostgreSQLDialect;
27+
import org.hibernate.engine.jdbc.spi.JdbcServices;
28+
import org.hibernate.engine.spi.SessionFactoryImplementor;
29+
import org.hibernate.integrator.spi.Integrator;
30+
import org.hibernate.mapping.PersistentClass;
31+
import org.hibernate.mapping.Property;
32+
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
33+
import org.springframework.stereotype.Component;
34+
35+
/**
36+
* auth: dahua time: 20250809
37+
*/
38+
@Component
39+
public class DynamicColumnIntegrator implements Integrator {
40+
41+
@Override
42+
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
43+
SessionFactoryServiceRegistry serviceRegistry) {
44+
JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
45+
Dialect dialect = jdbcServices.getDialect();
46+
PersistentClass persistentClass = metadata.getEntityBinding(PlanExecutionRecordEntity.class.getName());
47+
if (persistentClass == null) {
48+
return;
49+
}
50+
Property contentProperty = persistentClass.getProperty("planExecutionRecord");
51+
if (contentProperty == null) {
52+
return;
53+
}
54+
contentProperty.getColumns().forEach(column -> {
55+
if (dialect instanceof PostgreSQLDialect) { // pg
56+
column.setSqlType("TEXT");
57+
}
58+
else if (dialect instanceof MySQLDialect) { // mysql
59+
column.setSqlType("LONGTEXT");
60+
}
61+
else if (dialect instanceof H2Dialect) { // h2
62+
column.setSqlType("LONGTEXT");
63+
}
64+
});
65+
}
66+
67+
@Override
68+
public void disintegrate(@UnknownKeyFor @NonNull @Initialized SessionFactoryImplementor sessionFactory,
69+
@UnknownKeyFor @NonNull @Initialized SessionFactoryServiceRegistry serviceRegistry) {
70+
}
71+
72+
}

src/main/java/com/alibaba/cloud/ai/example/manus/dynamic/model/entity/DynamicModelEntity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public class DynamicModelEntity {
5050
@Column(nullable = false, columnDefinition = "boolean default false")
5151
private boolean isDefault;
5252

53-
@Column(nullable = true, columnDefinition = "DOUBLE DEFAULT 0.7")
53+
@Column
5454
private Double temperature;
5555

56-
@Column(nullable = true, columnDefinition = "DOUBLE DEFAULT NULL")
56+
@Column
5757
private Double topP;
5858

59-
@Column(nullable = true)
59+
@Column
6060
private String completionsPath;
6161

6262
public DynamicModelEntity() {

src/main/java/com/alibaba/cloud/ai/example/manus/dynamic/model/service/ModelServiceImpl.java

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,7 @@
1515
*/
1616
package com.alibaba.cloud.ai.example.manus.dynamic.model.service;
1717

18-
import java.net.MalformedURLException;
19-
import java.net.URL;
20-
import java.util.ArrayList;
21-
import java.util.List;
22-
import java.util.Map;
23-
import java.util.Optional;
24-
import java.util.stream.Collectors;
25-
26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
import org.springframework.beans.factory.annotation.Autowired;
29-
import org.springframework.http.HttpEntity;
30-
import org.springframework.http.HttpHeaders;
31-
import org.springframework.http.HttpMethod;
32-
import org.springframework.http.ResponseEntity;
33-
import org.springframework.stereotype.Service;
34-
import org.springframework.transaction.annotation.Transactional;
35-
import org.springframework.web.client.RestTemplate;
36-
18+
import cn.hutool.core.util.StrUtil;
3719
import com.alibaba.cloud.ai.example.manus.dynamic.agent.entity.DynamicAgentEntity;
3820
import com.alibaba.cloud.ai.example.manus.dynamic.agent.repository.DynamicAgentRepository;
3921
import com.alibaba.cloud.ai.example.manus.dynamic.model.entity.DynamicModelEntity;
@@ -46,8 +28,25 @@
4628
import com.alibaba.cloud.ai.example.manus.dynamic.model.repository.DynamicModelRepository;
4729
import com.alibaba.cloud.ai.example.manus.event.JmanusEventPublisher;
4830
import com.alibaba.cloud.ai.example.manus.event.ModelChangeEvent;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
import org.springframework.beans.factory.annotation.Autowired;
34+
import org.springframework.http.HttpEntity;
35+
import org.springframework.http.HttpHeaders;
36+
import org.springframework.http.HttpMethod;
37+
import org.springframework.http.ResponseEntity;
38+
import org.springframework.stereotype.Service;
39+
import org.springframework.transaction.annotation.Transactional;
40+
import org.springframework.util.StringUtils;
41+
import org.springframework.web.client.RestTemplate;
4942

50-
import cn.hutool.core.util.StrUtil;
43+
import java.net.MalformedURLException;
44+
import java.net.URL;
45+
import java.util.ArrayList;
46+
import java.util.List;
47+
import java.util.Map;
48+
import java.util.Optional;
49+
import java.util.stream.Collectors;
5150

5251
@Service
5352
public class ModelServiceImpl implements ModelService {
@@ -82,6 +81,8 @@ public ModelConfig getModelById(String id) {
8281
@Override
8382
public ModelConfig createModel(ModelConfig config) {
8483
try {
84+
// Set default values
85+
setDefaultConfig(config);
8586
// Check if an Model with the same name already exists
8687
DynamicModelEntity existingModel = repository.findByModelName(config.getModelName());
8788
if (existingModel != null) {
@@ -119,6 +120,7 @@ public ModelConfig createModel(ModelConfig config) {
119120

120121
@Override
121122
public ModelConfig updateModel(ModelConfig config) {
123+
setDefaultConfig(config);
122124
DynamicModelEntity entity = repository.findById(config.getId())
123125
.orElseThrow(() -> new IllegalArgumentException("Model not found: " + config.getId()));
124126

@@ -348,6 +350,15 @@ private void updateEntityFromConfig(DynamicModelEntity entity, ModelConfig confi
348350
entity.setCompletionsPath(config.getCompletionsPath());
349351
}
350352

353+
private void setDefaultConfig(ModelConfig modelConfig) {
354+
if (modelConfig.getTemperature() == null) {
355+
modelConfig.setTemperature(0.7);
356+
}
357+
if (!StringUtils.hasText(modelConfig.getCompletionsPath())) {
358+
modelConfig.setCompletionsPath("/v1/chat/completions");
359+
}
360+
}
361+
351362
@Override
352363
@Transactional
353364
public void setDefaultModel(Long modelId) {

src/main/java/com/alibaba/cloud/ai/example/manus/llm/LlmService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.springframework.stereotype.Service;
4545
import org.springframework.util.LinkedMultiValueMap;
4646
import org.springframework.util.MultiValueMap;
47-
import org.springframework.util.StringUtils;
4847
import org.springframework.web.client.RestClient;
4948
import org.springframework.web.reactive.function.client.WebClient;
5049
import reactor.core.publisher.Flux;
@@ -438,9 +437,6 @@ private OpenAiApi openAiApi(RestClient.Builder restClientBuilder, WebClient.Buil
438437
.filter((request, next) -> next.exchange(request).timeout(Duration.ofMinutes(10)));
439438

440439
String completionsPath = dynamicModelEntity.getCompletionsPath();
441-
if (!StringUtils.hasText(completionsPath)) {
442-
completionsPath = "/v1/chat/completions";
443-
}
444440

445441
return new OpenAiApi(dynamicModelEntity.getBaseUrl(), new SimpleApiKey(dynamicModelEntity.getApiKey()),
446442
multiValueMap, completionsPath, "/v1/embeddings", restClientBuilder, enhancedWebClientBuilder,

src/main/java/com/alibaba/cloud/ai/example/manus/recorder/entity/PlanExecutionRecordEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class PlanExecutionRecordEntity {
4242
private Date gmtModified;
4343

4444
@Convert(converter = StringAttributeConverter.class)
45-
@Column(columnDefinition = "longtext")
45+
@Column
4646
private PlanExecutionRecord planExecutionRecord;
4747

4848
public Long getId() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.alibaba.cloud.ai.example.manus.dynamic.dialect.DynamicColumnIntegrator

0 commit comments

Comments
 (0)