(improvement)(build) Add spotless during the build process. (#1639)

This commit is contained in:
lexluo09
2024-09-07 00:36:17 +08:00
committed by GitHub
parent ee15a88b06
commit 5f59e89eea
986 changed files with 15609 additions and 12706 deletions

View File

@@ -1,16 +1,15 @@
package com.tencent.supersonic.auth.api.authentication.adaptor; package com.tencent.supersonic.auth.api.authentication.adaptor;
import javax.servlet.http.HttpServletRequest;
import com.tencent.supersonic.auth.api.authentication.pojo.Organization; import com.tencent.supersonic.auth.api.authentication.pojo.Organization;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.request.UserReq; import com.tencent.supersonic.auth.api.authentication.request.UserReq;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.servlet.http.HttpServletRequest;
/** /** UserAdaptor defines some interfaces for obtaining user and organization information */
* UserAdaptor defines some interfaces for obtaining user and organization information
*/
public interface UserAdaptor { public interface UserAdaptor {
List<String> getUserNames(); List<String> getUserNames();

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.auth.api.authentication.annotation; package com.tencent.supersonic.auth.api.authentication.annotation;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@@ -8,6 +7,4 @@ import java.lang.annotation.Target;
@Target({ElementType.METHOD}) @Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface AuthenticationIgnore { public @interface AuthenticationIgnore {}
}

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.auth.api.authentication.config; package com.tencent.supersonic.auth.api.authentication.config;
import lombok.Data; import lombok.Data;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -25,8 +24,9 @@ public class AuthenticationConfig {
@Value("${s2.authentication.token.default.appKey:supersonic}") @Value("${s2.authentication.token.default.appKey:supersonic}")
private String tokenDefaultAppKey; private String tokenDefaultAppKey;
@Value("${s2.authentication.token.appSecret:supersonic:WIaO9YRRVt+7QtpPvyWsARFngnEcbaKBk" @Value(
+ "783uGFwMrbJBaochsqCH62L4Kijcb0sZCYoSsiKGV/zPml5MnZ3uQ==}") "${s2.authentication.token.appSecret:supersonic:WIaO9YRRVt+7QtpPvyWsARFngnEcbaKBk"
+ "783uGFwMrbJBaochsqCH62L4Kijcb0sZCYoSsiKGV/zPml5MnZ3uQ==}")
private String tokenAppSecret; private String tokenAppSecret;
@Value("${s2.authentication.token.http.header.key:Authorization}") @Value("${s2.authentication.token.http.header.key:Authorization}")

View File

@@ -17,5 +17,4 @@ public class UserConstants {
public static final String TOKEN_PREFIX = "Bearer"; public static final String TOKEN_PREFIX = "Bearer";
public static final String INTERNAL = "internal"; public static final String INTERNAL = "internal";
} }

View File

@@ -23,5 +23,4 @@ public class Organization {
private List<Organization> subOrganizations = Lists.newArrayList(); private List<Organization> subOrganizations = Lists.newArrayList();
private boolean isRoot; private boolean isRoot;
} }

View File

@@ -20,7 +20,8 @@ public class User {
private Integer isAdmin; private Integer isAdmin;
public static User get(Long id, String name, String displayName, String email, Integer isAdmin) { public static User get(
Long id, String name, String displayName, String email, Integer isAdmin) {
return new User(id, name, displayName, email, isAdmin); return new User(id, name, displayName, email, isAdmin);
} }
@@ -44,5 +45,4 @@ public class User {
public boolean isSuperAdmin() { public boolean isSuperAdmin() {
return isAdmin != null && isAdmin == 1; return isAdmin != null && isAdmin == 1;
} }
} }

View File

@@ -9,14 +9,24 @@ public class UserWithPassword extends User {
private String password; private String password;
public UserWithPassword(Long id, String name, String displayName, String email, String password, Integer isAdmin) { public UserWithPassword(
Long id,
String name,
String displayName,
String email,
String password,
Integer isAdmin) {
super(id, name, displayName, email, isAdmin); super(id, name, displayName, email, isAdmin);
this.password = password; this.password = password;
} }
public static UserWithPassword get(Long id, String name, String displayName, public static UserWithPassword get(
String email, String password, Integer isAdmin) { Long id,
String name,
String displayName,
String email,
String password,
Integer isAdmin) {
return new UserWithPassword(id, name, displayName, email, password, isAdmin); return new UserWithPassword(id, name, displayName, email, password, isAdmin);
} }
} }

View File

@@ -1,7 +1,7 @@
package com.tencent.supersonic.auth.api.authentication.request; package com.tencent.supersonic.auth.api.authentication.request;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import lombok.Data; import lombok.Data;
@Data @Data
@@ -12,6 +12,4 @@ public class UserReq {
@NotBlank(message = "password can not be null") @NotBlank(message = "password can not be null")
private String password; private String password;
} }

View File

@@ -1,17 +1,19 @@
package com.tencent.supersonic.auth.api.authentication.service; package com.tencent.supersonic.auth.api.authentication.service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.Organization; import com.tencent.supersonic.auth.api.authentication.pojo.Organization;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.request.UserReq; import com.tencent.supersonic.auth.api.authentication.request.UserReq;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public interface UserService { public interface UserService {
User getCurrentUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse); User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);
List<String> getUserNames(); List<String> getUserNames();

View File

@@ -1,10 +1,10 @@
package com.tencent.supersonic.auth.api.authentication.service; package com.tencent.supersonic.auth.api.authentication.service;
import com.tencent.supersonic.auth.api.authentication.pojo.User;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.User;
public interface UserStrategy { public interface UserStrategy {
boolean accept(boolean isEnableAuthentication); boolean accept(boolean isEnableAuthentication);
@@ -12,5 +12,4 @@ public interface UserStrategy {
User findUser(HttpServletRequest request, HttpServletResponse response); User findUser(HttpServletRequest request, HttpServletResponse response);
User findUser(String token, String appKey); User findUser(String token, String appKey);
} }

View File

@@ -1,12 +1,13 @@
package com.tencent.supersonic.auth.api.authentication.utils; package com.tencent.supersonic.auth.api.authentication.utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.service.UserStrategy; import com.tencent.supersonic.auth.api.authentication.service.UserStrategy;
import com.tencent.supersonic.common.config.SystemConfig; import com.tencent.supersonic.common.config.SystemConfig;
import com.tencent.supersonic.common.service.SystemConfigService; import com.tencent.supersonic.common.service.SystemConfigService;
import com.tencent.supersonic.common.util.ContextUtils; import com.tencent.supersonic.common.util.ContextUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
public final class UserHolder { public final class UserHolder {
@@ -36,5 +37,4 @@ public final class UserHolder {
} }
return user; return user;
} }
} }

View File

@@ -1,8 +1,9 @@
package com.tencent.supersonic.auth.api.authorization.pojo; package com.tencent.supersonic.auth.api.authorization.pojo;
import java.util.List;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class AuthGroup { public class AuthGroup {
@@ -10,18 +11,12 @@ public class AuthGroup {
private String name; private String name;
private Integer groupId; private Integer groupId;
private List<AuthRule> authRules; private List<AuthRule> authRules;
/** /** row permission expression */
* row permission expression
*/
private List<String> dimensionFilters; private List<String> dimensionFilters;
/** /** row permission expression description information */
* row permission expression description information
*/
private String dimensionFilterDescription; private String dimensionFilterDescription;
private List<String> authorizedUsers; private List<String> authorizedUsers;
/** /** authorization Department Id */
* authorization Department Id
*/
private List<String> authorizedDepartmentIds; private List<String> authorizedDepartmentIds;
} }

View File

@@ -10,8 +10,7 @@ public class AuthRes {
private Long modelId; private Long modelId;
private String name; private String name;
public AuthRes() { public AuthRes() {}
}
public AuthRes(Long modelId, String name) { public AuthRes(Long modelId, String name) {
this.modelId = modelId; this.modelId = modelId;

View File

@@ -1,9 +1,10 @@
package com.tencent.supersonic.auth.api.authorization.pojo; package com.tencent.supersonic.auth.api.authorization.pojo;
import lombok.Data;
import java.beans.Transient; import java.beans.Transient;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import lombok.Data;
@Data @Data
public class AuthRule { public class AuthRule {

View File

@@ -1,8 +1,9 @@
package com.tencent.supersonic.auth.api.authorization.pojo; package com.tencent.supersonic.auth.api.authorization.pojo;
import lombok.Data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import lombok.Data;
@Data @Data
public class DimensionFilter { public class DimensionFilter {

View File

@@ -1,8 +1,9 @@
package com.tencent.supersonic.auth.api.authorization.request; package com.tencent.supersonic.auth.api.authorization.request;
import java.util.List;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class AddUsersToGroupReq { public class AddUsersToGroupReq {

View File

@@ -1,10 +1,10 @@
package com.tencent.supersonic.auth.api.authorization.request; package com.tencent.supersonic.auth.api.authorization.request;
import com.tencent.supersonic.common.pojo.PageBaseReq; import com.tencent.supersonic.common.pojo.PageBaseReq;
import java.util.List;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class QueryGroupReq extends PageBaseReq { public class QueryGroupReq extends PageBaseReq {

View File

@@ -1,8 +1,9 @@
package com.tencent.supersonic.auth.api.authorization.request; package com.tencent.supersonic.auth.api.authorization.request;
import java.util.List;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class RemoveGroupReq { public class RemoveGroupReq {

View File

@@ -1,8 +1,9 @@
package com.tencent.supersonic.auth.api.authorization.request; package com.tencent.supersonic.auth.api.authorization.request;
import java.util.List;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class RemoveUsersFromGroupReq { public class RemoveUsersFromGroupReq {

View File

@@ -4,6 +4,7 @@ import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authorization.pojo.AuthGroup; import com.tencent.supersonic.auth.api.authorization.pojo.AuthGroup;
import com.tencent.supersonic.auth.api.authorization.request.QueryAuthResReq; import com.tencent.supersonic.auth.api.authorization.request.QueryAuthResReq;
import com.tencent.supersonic.auth.api.authorization.response.AuthorizedResourceResp; import com.tencent.supersonic.auth.api.authorization.response.AuthorizedResourceResp;
import java.util.List; import java.util.List;
public interface AuthService { public interface AuthService {

View File

@@ -1,5 +1,7 @@
package com.tencent.supersonic.auth.authentication.adaptor; package com.tencent.supersonic.auth.authentication.adaptor;
import javax.servlet.http.HttpServletRequest;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.tencent.supersonic.auth.api.authentication.adaptor.UserAdaptor; import com.tencent.supersonic.auth.api.authentication.adaptor.UserAdaptor;
@@ -9,20 +11,17 @@ import com.tencent.supersonic.auth.api.authentication.pojo.UserWithPassword;
import com.tencent.supersonic.auth.api.authentication.request.UserReq; import com.tencent.supersonic.auth.api.authentication.request.UserReq;
import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDO; import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDO;
import com.tencent.supersonic.auth.authentication.persistence.repository.UserRepository; import com.tencent.supersonic.auth.authentication.persistence.repository.UserRepository;
import com.tencent.supersonic.common.util.AESEncryptionUtil;
import com.tencent.supersonic.auth.authentication.utils.UserTokenUtils; import com.tencent.supersonic.auth.authentication.utils.UserTokenUtils;
import com.tencent.supersonic.common.util.AESEncryptionUtil;
import com.tencent.supersonic.common.util.ContextUtils; import com.tencent.supersonic.common.util.ContextUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /** DefaultUserAdaptor provides a default method to obtain user and organization information */
* DefaultUserAdaptor provides a default method to obtain user and organization information
*/
@Slf4j @Slf4j
public class DefaultUserAdaptor implements UserAdaptor { public class DefaultUserAdaptor implements UserAdaptor {
@@ -49,14 +48,16 @@ public class DefaultUserAdaptor implements UserAdaptor {
@Override @Override
public List<Organization> getOrganizationTree() { public List<Organization> getOrganizationTree() {
Organization superSonic = new Organization("1", "0", Organization superSonic =
"SuperSonic", "SuperSonic", Lists.newArrayList(), true); new Organization("1", "0", "SuperSonic", "SuperSonic", Lists.newArrayList(), true);
Organization hr = new Organization("2", "1", Organization hr =
"Hr", "SuperSonic/Hr", Lists.newArrayList(), false); new Organization("2", "1", "Hr", "SuperSonic/Hr", Lists.newArrayList(), false);
Organization sales = new Organization("3", "1", Organization sales =
"Sales", "SuperSonic/Sales", Lists.newArrayList(), false); new Organization(
Organization marketing = new Organization("4", "1", "3", "1", "Sales", "SuperSonic/Sales", Lists.newArrayList(), false);
"Marketing", "SuperSonic/Marketing", Lists.newArrayList(), false); Organization marketing =
new Organization(
"4", "1", "Marketing", "SuperSonic/Marketing", Lists.newArrayList(), false);
List<Organization> subOrganization = Lists.newArrayList(hr, sales, marketing); List<Organization> subOrganization = Lists.newArrayList(hr, sales, marketing);
superSonic.setSubOrganizations(subOrganization); superSonic.setSubOrganizations(subOrganization);
return Lists.newArrayList(superSonic); return Lists.newArrayList(superSonic);
@@ -112,11 +113,19 @@ public class DefaultUserAdaptor implements UserAdaptor {
throw new RuntimeException("user not exist,please register"); throw new RuntimeException("user not exist,please register");
} }
try { try {
String password = AESEncryptionUtil.encrypt(userReq.getPassword(), String password =
AESEncryptionUtil.getBytesFromString(userDO.getSalt())); AESEncryptionUtil.encrypt(
userReq.getPassword(),
AESEncryptionUtil.getBytesFromString(userDO.getSalt()));
if (userDO.getPassword().equals(password)) { if (userDO.getPassword().equals(password)) {
UserWithPassword user = UserWithPassword.get(userDO.getId(), userDO.getName(), userDO.getDisplayName(), UserWithPassword user =
userDO.getEmail(), userDO.getPassword(), userDO.getIsAdmin()); UserWithPassword.get(
userDO.getId(),
userDO.getName(),
userDO.getDisplayName(),
userDO.getEmail(),
userDO.getPassword(),
userDO.getIsAdmin());
return user; return user;
} else { } else {
throw new RuntimeException("password not correct, please try again"); throw new RuntimeException("password not correct, please try again");
@@ -135,5 +144,4 @@ public class DefaultUserAdaptor implements UserAdaptor {
public Set<String> getUserAllOrgId(String userName) { public Set<String> getUserAllOrgId(String userName) {
return Sets.newHashSet(); return Sets.newHashSet();
} }
} }

View File

@@ -1,5 +1,7 @@
package com.tencent.supersonic.auth.authentication.interceptor; package com.tencent.supersonic.auth.authentication.interceptor;
import javax.servlet.http.HttpServletRequest;
import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig; import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig;
import com.tencent.supersonic.auth.api.authentication.constant.UserConstants; import com.tencent.supersonic.auth.api.authentication.constant.UserConstants;
import com.tencent.supersonic.auth.authentication.service.UserServiceImpl; import com.tencent.supersonic.auth.authentication.service.UserServiceImpl;
@@ -13,7 +15,6 @@ import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -21,14 +22,12 @@ import java.util.List;
@Slf4j @Slf4j
public abstract class AuthenticationInterceptor implements HandlerInterceptor { public abstract class AuthenticationInterceptor implements HandlerInterceptor {
protected AuthenticationConfig authenticationConfig; protected AuthenticationConfig authenticationConfig;
protected UserServiceImpl userServiceImpl; protected UserServiceImpl userServiceImpl;
protected UserTokenUtils userTokenUtils; protected UserTokenUtils userTokenUtils;
protected S2ThreadContext s2ThreadContext; protected S2ThreadContext s2ThreadContext;
protected boolean isExcludedUri(String uri) { protected boolean isExcludedUri(String uri) {
@@ -69,7 +68,8 @@ public abstract class AuthenticationInterceptor implements HandlerInterceptor {
try { try {
if (request instanceof StandardMultipartHttpServletRequest) { if (request instanceof StandardMultipartHttpServletRequest) {
RequestFacade servletRequest = RequestFacade servletRequest =
(RequestFacade) ((StandardMultipartHttpServletRequest) request).getRequest(); (RequestFacade)
((StandardMultipartHttpServletRequest) request).getRequest();
Class<? extends HttpServletRequest> servletRequestClazz = servletRequest.getClass(); Class<? extends HttpServletRequest> servletRequestClazz = servletRequest.getClass();
Field request1 = servletRequestClazz.getDeclaredField("request"); Field request1 = servletRequestClazz.getDeclaredField("request");
request1.setAccessible(true); request1.setAccessible(true);

View File

@@ -1,5 +1,7 @@
package com.tencent.supersonic.auth.authentication.interceptor; package com.tencent.supersonic.auth.authentication.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.annotation.AuthenticationIgnore; import com.tencent.supersonic.auth.api.authentication.annotation.AuthenticationIgnore;
import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig; import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig;
@@ -14,15 +16,15 @@ import com.tencent.supersonic.common.util.ThreadContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@Slf4j @Slf4j
public class DefaultAuthenticationInterceptor extends AuthenticationInterceptor { public class DefaultAuthenticationInterceptor extends AuthenticationInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) public boolean preHandle(
HttpServletRequest request, HttpServletResponse response, Object handler)
throws AccessException { throws AccessException {
authenticationConfig = ContextUtils.getBean(AuthenticationConfig.class); authenticationConfig = ContextUtils.getBean(AuthenticationConfig.class);
userServiceImpl = ContextUtils.getBean(UserServiceImpl.class); userServiceImpl = ContextUtils.getBean(UserServiceImpl.class);
@@ -73,11 +75,11 @@ public class DefaultAuthenticationInterceptor extends AuthenticationInterceptor
} }
private void setContext(String userName, HttpServletRequest request) { private void setContext(String userName, HttpServletRequest request) {
ThreadContext threadContext = ThreadContext.builder() ThreadContext threadContext =
.token(request.getHeader(authenticationConfig.getTokenHttpHeaderKey())) ThreadContext.builder()
.userName(userName) .token(request.getHeader(authenticationConfig.getTokenHttpHeaderKey()))
.build(); .userName(userName)
.build();
s2ThreadContext.set(threadContext); s2ThreadContext.set(threadContext);
} }
} }

View File

@@ -10,20 +10,21 @@ import java.util.List;
@Configuration @Configuration
public class InterceptorFactory implements WebMvcConfigurer { public class InterceptorFactory implements WebMvcConfigurer {
private List<AuthenticationInterceptor> authenticationInterceptors; private List<AuthenticationInterceptor> authenticationInterceptors;
public InterceptorFactory() { public InterceptorFactory() {
authenticationInterceptors = SpringFactoriesLoader.loadFactories(AuthenticationInterceptor.class, authenticationInterceptors =
Thread.currentThread().getContextClassLoader()); SpringFactoriesLoader.loadFactories(
AuthenticationInterceptor.class,
Thread.currentThread().getContextClassLoader());
} }
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
for (AuthenticationInterceptor authenticationInterceptor : authenticationInterceptors) { for (AuthenticationInterceptor authenticationInterceptor : authenticationInterceptors) {
registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**") registry.addInterceptor(authenticationInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/", "/webapp/**", "/error"); .excludePathPatterns("/", "/webapp/**", "/error");
} }
} }
} }

View File

@@ -1,82 +1,52 @@
package com.tencent.supersonic.auth.authentication.persistence.dataobject; package com.tencent.supersonic.auth.authentication.persistence.dataobject;
public class UserDO { public class UserDO {
/** /** */
*
*/
private Long id; private Long id;
/** /** */
*
*/
private String name; private String name;
/** /** */
*
*/
private String password; private String password;
private String salt; private String salt;
/** /** */
*
*/
private String displayName; private String displayName;
/** /** */
*
*/
private String email; private String email;
/** /** */
*
*/
private Integer isAdmin; private Integer isAdmin;
/** /** @return id */
*
* @return id
*/
public Long getId() { public Long getId() {
return id; return id;
} }
/** /** @param id */
*
* @param id
*/
public void setId(Long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
/** /** @return name */
*
* @return name
*/
public String getName() { public String getName() {
return name; return name;
} }
/** /** @param name */
*
* @param name
*/
public void setName(String name) { public void setName(String name) {
this.name = name == null ? null : name.trim(); this.name = name == null ? null : name.trim();
} }
/** /** @return password */
*
* @return password
*/
public String getPassword() { public String getPassword() {
return password; return password;
} }
/** /** @param password */
*
* @param password
*/
public void setPassword(String password) { public void setPassword(String password) {
this.password = password == null ? null : password.trim(); this.password = password == null ? null : password.trim();
} }
@@ -89,50 +59,32 @@ public class UserDO {
this.salt = salt == null ? null : salt.trim(); this.salt = salt == null ? null : salt.trim();
} }
/** /** @return display_name */
*
* @return display_name
*/
public String getDisplayName() { public String getDisplayName() {
return displayName; return displayName;
} }
/** /** @param displayName */
*
* @param displayName
*/
public void setDisplayName(String displayName) { public void setDisplayName(String displayName) {
this.displayName = displayName == null ? null : displayName.trim(); this.displayName = displayName == null ? null : displayName.trim();
} }
/** /** @return email */
*
* @return email
*/
public String getEmail() { public String getEmail() {
return email; return email;
} }
/** /** @param email */
*
* @param email
*/
public void setEmail(String email) { public void setEmail(String email) {
this.email = email == null ? null : email.trim(); this.email = email == null ? null : email.trim();
} }
/** /** @return is_admin */
*
* @return is_admin
*/
public Integer getIsAdmin() { public Integer getIsAdmin() {
return isAdmin; return isAdmin;
} }
/** /** @param isAdmin */
*
* @param isAdmin
*/
public void setIsAdmin(Integer isAdmin) { public void setIsAdmin(Integer isAdmin) {
this.isAdmin = isAdmin; this.isAdmin = isAdmin;
} }

View File

@@ -4,101 +4,64 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class UserDOExample { public class UserDOExample {
/** /** s2_user */
* s2_user
*/
protected String orderByClause; protected String orderByClause;
/** /** s2_user */
* s2_user
*/
protected boolean distinct; protected boolean distinct;
/** /** s2_user */
* s2_user
*/
protected List<Criteria> oredCriteria; protected List<Criteria> oredCriteria;
/** /** s2_user */
* s2_user
*/
protected Integer limitStart; protected Integer limitStart;
/** /** s2_user */
* s2_user
*/
protected Integer limitEnd; protected Integer limitEnd;
/** /** @mbg.generated */
*
* @mbg.generated
*/
public UserDOExample() { public UserDOExample() {
oredCriteria = new ArrayList<Criteria>(); oredCriteria = new ArrayList<Criteria>();
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public void setOrderByClause(String orderByClause) { public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause; this.orderByClause = orderByClause;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public String getOrderByClause() { public String getOrderByClause() {
return orderByClause; return orderByClause;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public void setDistinct(boolean distinct) { public void setDistinct(boolean distinct) {
this.distinct = distinct; this.distinct = distinct;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public boolean isDistinct() { public boolean isDistinct() {
return distinct; return distinct;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public List<Criteria> getOredCriteria() { public List<Criteria> getOredCriteria() {
return oredCriteria; return oredCriteria;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public void or(Criteria criteria) { public void or(Criteria criteria) {
oredCriteria.add(criteria); oredCriteria.add(criteria);
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public Criteria or() { public Criteria or() {
Criteria criteria = createCriteriaInternal(); Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria); oredCriteria.add(criteria);
return criteria; return criteria;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public Criteria createCriteria() { public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal(); Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) { if (oredCriteria.size() == 0) {
@@ -107,60 +70,40 @@ public class UserDOExample {
return criteria; return criteria;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
protected Criteria createCriteriaInternal() { protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria(); Criteria criteria = new Criteria();
return criteria; return criteria;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public void clear() { public void clear() {
oredCriteria.clear(); oredCriteria.clear();
orderByClause = null; orderByClause = null;
distinct = false; distinct = false;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public void setLimitStart(Integer limitStart) { public void setLimitStart(Integer limitStart) {
this.limitStart=limitStart; this.limitStart = limitStart;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public Integer getLimitStart() { public Integer getLimitStart() {
return limitStart; return limitStart;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public void setLimitEnd(Integer limitEnd) { public void setLimitEnd(Integer limitEnd) {
this.limitEnd=limitEnd; this.limitEnd = limitEnd;
} }
/** /** @mbg.generated */
*
* @mbg.generated
*/
public Integer getLimitEnd() { public Integer getLimitEnd() {
return limitEnd; return limitEnd;
} }
/** /** s2_user null */
* s2_user null
*/
protected abstract static class GeneratedCriteria { protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria; protected List<Criterion> criteria;
@@ -195,7 +138,8 @@ public class UserDOExample {
criteria.add(new Criterion(condition, value)); criteria.add(new Criterion(condition, value));
} }
protected void addCriterion(String condition, Object value1, Object value2, String property) { protected void addCriterion(
String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) { if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null"); throw new RuntimeException("Between values for " + property + " cannot be null");
} }
@@ -603,9 +547,7 @@ public class UserDOExample {
} }
} }
/** /** s2_user */
* s2_user
*/
public static class Criteria extends GeneratedCriteria { public static class Criteria extends GeneratedCriteria {
protected Criteria() { protected Criteria() {
@@ -613,9 +555,7 @@ public class UserDOExample {
} }
} }
/** /** s2_user null */
* s2_user null
*/
public static class Criterion { public static class Criterion {
private String condition; private String condition;
@@ -688,7 +628,8 @@ public class UserDOExample {
this(condition, value, null); this(condition, value, null);
} }
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { protected Criterion(
String condition, Object value, Object secondValue, String typeHandler) {
super(); super();
this.condition = condition; this.condition = condition;
this.value = value; this.value = value;

View File

@@ -1,22 +1,17 @@
package com.tencent.supersonic.auth.authentication.persistence.mapper; package com.tencent.supersonic.auth.authentication.persistence.mapper;
import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDO; import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDO;
import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDOExample; import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDOExample;
import java.util.List;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper @Mapper
public interface UserDOMapper { public interface UserDOMapper {
/** /** @mbg.generated */
* @mbg.generated
*/
int insert(UserDO record); int insert(UserDO record);
/** /** @mbg.generated */
* @mbg.generated
*/
List<UserDO> selectByExample(UserDOExample example); List<UserDO> selectByExample(UserDOExample example);
} }

View File

@@ -1,6 +1,7 @@
package com.tencent.supersonic.auth.authentication.persistence.repository; package com.tencent.supersonic.auth.authentication.persistence.repository;
import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDO; import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDO;
import java.util.List; import java.util.List;
public interface UserRepository { public interface UserRepository {

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.auth.authentication.persistence.repository.impl; package com.tencent.supersonic.auth.authentication.persistence.repository.impl;
import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDO; import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDO;
import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDOExample; import com.tencent.supersonic.auth.authentication.persistence.dataobject.UserDOExample;
import com.tencent.supersonic.auth.authentication.persistence.mapper.UserDOMapper; import com.tencent.supersonic.auth.authentication.persistence.mapper.UserDOMapper;
@@ -13,10 +12,8 @@ import java.util.Optional;
@Component @Component
public class UserRepositoryImpl implements UserRepository { public class UserRepositoryImpl implements UserRepository {
private UserDOMapper userDOMapper; private UserDOMapper userDOMapper;
public UserRepositoryImpl(UserDOMapper userDOMapper) { public UserRepositoryImpl(UserDOMapper userDOMapper) {
this.userDOMapper = userDOMapper; this.userDOMapper = userDOMapper;
} }
@@ -39,5 +36,4 @@ public class UserRepositoryImpl implements UserRepository {
Optional<UserDO> userDOOptional = userDOS.stream().findFirst(); Optional<UserDO> userDOOptional = userDOS.stream().findFirst();
return userDOOptional.orElse(null); return userDOOptional.orElse(null);
} }
} }

View File

@@ -1,5 +1,7 @@
package com.tencent.supersonic.auth.authentication.rest; package com.tencent.supersonic.auth.authentication.rest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.Organization; import com.tencent.supersonic.auth.api.authentication.pojo.Organization;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
@@ -13,8 +15,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -30,7 +30,8 @@ public class UserController {
} }
@GetMapping("/getCurrentUser") @GetMapping("/getCurrentUser")
public User getCurrentUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { public User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
return userService.getCurrentUser(httpServletRequest, httpServletResponse); return userService.getCurrentUser(httpServletRequest, httpServletResponse);
} }
@@ -68,5 +69,4 @@ public class UserController {
public String login(@RequestBody UserReq userCmd, HttpServletRequest request) { public String login(@RequestBody UserReq userCmd, HttpServletRequest request) {
return userService.login(userCmd, request); return userService.login(userCmd, request);
} }
} }

View File

@@ -1,5 +1,8 @@
package com.tencent.supersonic.auth.authentication.service; package com.tencent.supersonic.auth.authentication.service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.Organization; import com.tencent.supersonic.auth.api.authentication.pojo.Organization;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.request.UserReq; import com.tencent.supersonic.auth.api.authentication.request.UserReq;
@@ -10,8 +13,7 @@ import com.tencent.supersonic.common.config.SystemConfig;
import com.tencent.supersonic.common.service.SystemConfigService; import com.tencent.supersonic.common.service.SystemConfigService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -25,7 +27,8 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public User getCurrentUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { public User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
User user = UserHolder.findUser(httpServletRequest, httpServletResponse); User user = UserHolder.findUser(httpServletRequest, httpServletResponse);
if (user != null) { if (user != null) {
SystemConfig systemConfig = sysParameterService.getSystemConfig(); SystemConfig systemConfig = sysParameterService.getSystemConfig();
@@ -76,5 +79,4 @@ public class UserServiceImpl implements UserService {
public String login(UserReq userReq, String appKey) { public String login(UserReq userReq, String appKey) {
return ComponentFactory.getUserAdaptor().login(userReq, appKey); return ComponentFactory.getUserAdaptor().login(userReq, appKey);
} }
} }

View File

@@ -1,10 +1,10 @@
package com.tencent.supersonic.auth.authentication.strategy; package com.tencent.supersonic.auth.authentication.strategy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.service.UserStrategy; import com.tencent.supersonic.auth.api.authentication.service.UserStrategy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@@ -24,5 +24,4 @@ public class FakeUserStrategy implements UserStrategy {
public User findUser(String token, String appKey) { public User findUser(String token, String appKey) {
return User.getFakeUser(); return User.getFakeUser();
} }
} }

View File

@@ -1,20 +1,18 @@
package com.tencent.supersonic.auth.authentication.strategy; package com.tencent.supersonic.auth.authentication.strategy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.service.UserStrategy; import com.tencent.supersonic.auth.api.authentication.service.UserStrategy;
import com.tencent.supersonic.auth.authentication.utils.UserTokenUtils; import com.tencent.supersonic.auth.authentication.utils.UserTokenUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class HttpHeaderUserStrategy implements UserStrategy { public class HttpHeaderUserStrategy implements UserStrategy {
private final UserTokenUtils userTokenUtils; private final UserTokenUtils userTokenUtils;
public HttpHeaderUserStrategy(UserTokenUtils userTokenUtils) { public HttpHeaderUserStrategy(UserTokenUtils userTokenUtils) {
this.userTokenUtils = userTokenUtils; this.userTokenUtils = userTokenUtils;
} }

View File

@@ -1,26 +1,25 @@
package com.tencent.supersonic.auth.authentication.strategy; package com.tencent.supersonic.auth.authentication.strategy;
import javax.annotation.PostConstruct;
import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig; import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig;
import com.tencent.supersonic.auth.api.authentication.service.UserStrategy; import com.tencent.supersonic.auth.api.authentication.service.UserStrategy;
import com.tencent.supersonic.auth.api.authentication.utils.UserHolder; import com.tencent.supersonic.auth.api.authentication.utils.UserHolder;
import java.util.List;
import javax.annotation.PostConstruct;
import lombok.Data; import lombok.Data;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.util.List;
@Configuration @Configuration
@Data @Data
public class UserStrategyFactory { public class UserStrategyFactory {
private List<UserStrategy> userStrategyList; private List<UserStrategy> userStrategyList;
private AuthenticationConfig authenticationConfig; private AuthenticationConfig authenticationConfig;
public UserStrategyFactory(AuthenticationConfig authenticationConfig, List<UserStrategy> userStrategyList) { public UserStrategyFactory(
AuthenticationConfig authenticationConfig, List<UserStrategy> userStrategyList) {
this.authenticationConfig = authenticationConfig; this.authenticationConfig = authenticationConfig;
this.userStrategyList = userStrategyList; this.userStrategyList = userStrategyList;
} }

View File

@@ -2,6 +2,7 @@ package com.tencent.supersonic.auth.authentication.utils;
import com.tencent.supersonic.auth.api.authentication.adaptor.UserAdaptor; import com.tencent.supersonic.auth.api.authentication.adaptor.UserAdaptor;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
import java.util.Objects; import java.util.Objects;
public class ComponentFactory { public class ComponentFactory {
@@ -16,8 +17,8 @@ public class ComponentFactory {
} }
private static <T> T init(Class<T> factoryType) { private static <T> T init(Class<T> factoryType) {
return SpringFactoriesLoader.loadFactories(factoryType, return SpringFactoriesLoader.loadFactories(
Thread.currentThread().getContextClassLoader()).get(0); factoryType, Thread.currentThread().getContextClassLoader())
.get(0);
} }
} }

View File

@@ -1,5 +1,8 @@
package com.tencent.supersonic.auth.authentication.utils; package com.tencent.supersonic.auth.authentication.utils;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig; import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.pojo.UserWithPassword; import com.tencent.supersonic.auth.api.authentication.pojo.UserWithPassword;
@@ -11,8 +14,6 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@@ -46,7 +47,9 @@ public class UserTokenUtils {
Map<String, Object> claims = new HashMap<>(5); Map<String, Object> claims = new HashMap<>(5);
claims.put(TOKEN_USER_ID, user.getId()); claims.put(TOKEN_USER_ID, user.getId());
claims.put(TOKEN_USER_NAME, StringUtils.isEmpty(user.getName()) ? "" : user.getName()); claims.put(TOKEN_USER_NAME, StringUtils.isEmpty(user.getName()) ? "" : user.getName());
claims.put(TOKEN_USER_PASSWORD, StringUtils.isEmpty(user.getPassword()) ? "" : user.getPassword()); claims.put(
TOKEN_USER_PASSWORD,
StringUtils.isEmpty(user.getPassword()) ? "" : user.getPassword());
claims.put(TOKEN_USER_DISPLAY_NAME, user.getDisplayName()); claims.put(TOKEN_USER_DISPLAY_NAME, user.getDisplayName());
claims.put(TOKEN_CREATE_TIME, System.currentTimeMillis()); claims.put(TOKEN_CREATE_TIME, System.currentTimeMillis());
claims.put(TOKEN_IS_ADMIN, user.getIsAdmin()); claims.put(TOKEN_IS_ADMIN, user.getIsAdmin());
@@ -79,8 +82,10 @@ public class UserTokenUtils {
String userName = String.valueOf(claims.get(TOKEN_USER_NAME)); String userName = String.valueOf(claims.get(TOKEN_USER_NAME));
String email = String.valueOf(claims.get(TOKEN_USER_EMAIL)); String email = String.valueOf(claims.get(TOKEN_USER_EMAIL));
String displayName = String.valueOf(claims.get(TOKEN_USER_DISPLAY_NAME)); String displayName = String.valueOf(claims.get(TOKEN_USER_DISPLAY_NAME));
Integer isAdmin = claims.get(TOKEN_IS_ADMIN) == null Integer isAdmin =
? 0 : Integer.parseInt(claims.get(TOKEN_IS_ADMIN).toString()); claims.get(TOKEN_IS_ADMIN) == null
? 0
: Integer.parseInt(claims.get(TOKEN_IS_ADMIN).toString());
return User.get(userId, userName, displayName, email, isAdmin); return User.get(userId, userName, displayName, email, isAdmin);
} }
@@ -97,8 +102,10 @@ public class UserTokenUtils {
String email = String.valueOf(claims.get(TOKEN_USER_EMAIL)); String email = String.valueOf(claims.get(TOKEN_USER_EMAIL));
String displayName = String.valueOf(claims.get(TOKEN_USER_DISPLAY_NAME)); String displayName = String.valueOf(claims.get(TOKEN_USER_DISPLAY_NAME));
String password = String.valueOf(claims.get(TOKEN_USER_PASSWORD)); String password = String.valueOf(claims.get(TOKEN_USER_PASSWORD));
Integer isAdmin = claims.get(TOKEN_IS_ADMIN) == null Integer isAdmin =
? 0 : Integer.parseInt(claims.get(TOKEN_IS_ADMIN).toString()); claims.get(TOKEN_IS_ADMIN) == null
? 0
: Integer.parseInt(claims.get(TOKEN_IS_ADMIN).toString());
return UserWithPassword.get(userId, userName, displayName, email, password, isAdmin); return UserWithPassword.get(userId, userName, displayName, email, password, isAdmin);
} }
@@ -117,9 +124,12 @@ public class UserTokenUtils {
Claims claims; Claims claims;
try { try {
String tokenSecret = getTokenSecret(appKey); String tokenSecret = getTokenSecret(appKey);
claims = Jwts.parser() claims =
.setSigningKey(tokenSecret.getBytes(StandardCharsets.UTF_8)) Jwts.parser()
.build().parseClaimsJws(getTokenString(token)).getBody(); .setSigningKey(tokenSecret.getBytes(StandardCharsets.UTF_8))
.build()
.parseClaimsJws(getTokenString(token))
.getBody();
} catch (Exception e) { } catch (Exception e) {
log.error("getClaims", e); log.error("getClaims", e);
throw new AccessException("parse user info from token failed :" + token); throw new AccessException("parse user info from token failed :" + token);
@@ -128,8 +138,9 @@ public class UserTokenUtils {
} }
private static String getTokenString(String token) { private static String getTokenString(String token) {
return token.startsWith(TOKEN_PREFIX) ? token.substring(token.indexOf(TOKEN_PREFIX) return token.startsWith(TOKEN_PREFIX)
+ TOKEN_PREFIX.length()).trim() : token.trim(); ? token.substring(token.indexOf(TOKEN_PREFIX) + TOKEN_PREFIX.length()).trim()
: token.trim();
} }
private String generate(Map<String, Object> claims, String appKey) { private String generate(Map<String, Object> claims, String appKey) {
@@ -146,8 +157,11 @@ public class UserTokenUtils {
.setClaims(claims) .setClaims(claims)
.setSubject(claims.get(TOKEN_USER_NAME).toString()) .setSubject(claims.get(TOKEN_USER_NAME).toString())
.setExpiration(expirationDate) .setExpiration(expirationDate)
.signWith(new SecretKeySpec(tokenSecret.getBytes(StandardCharsets.UTF_8), .signWith(
SignatureAlgorithm.HS512.getJcaName()), SignatureAlgorithm.HS512) new SecretKeySpec(
tokenSecret.getBytes(StandardCharsets.UTF_8),
SignatureAlgorithm.HS512.getJcaName()),
SignatureAlgorithm.HS512)
.compact(); .compact();
} }

View File

@@ -1,14 +1,14 @@
package com.tencent.supersonic.auth.authorization.rest; package com.tencent.supersonic.auth.authorization.rest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.utils.UserHolder; import com.tencent.supersonic.auth.api.authentication.utils.UserHolder;
import com.tencent.supersonic.auth.api.authorization.pojo.AuthGroup;
import com.tencent.supersonic.auth.api.authorization.request.QueryAuthResReq; import com.tencent.supersonic.auth.api.authorization.request.QueryAuthResReq;
import com.tencent.supersonic.auth.api.authorization.response.AuthorizedResourceResp; import com.tencent.supersonic.auth.api.authorization.response.AuthorizedResourceResp;
import com.tencent.supersonic.auth.api.authorization.service.AuthService; import com.tencent.supersonic.auth.api.authorization.service.AuthService;
import com.tencent.supersonic.auth.api.authorization.pojo.AuthGroup;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RestController
@RequestMapping("/api/auth") @RequestMapping("/api/auth")
@Slf4j @Slf4j
@@ -29,14 +31,13 @@ public class AuthController {
} }
@GetMapping("/queryGroup") @GetMapping("/queryGroup")
public List<AuthGroup> queryAuthGroup(@RequestParam("modelId") String modelId, public List<AuthGroup> queryAuthGroup(
@RequestParam("modelId") String modelId,
@RequestParam(value = "groupId", required = false) Integer groupId) { @RequestParam(value = "groupId", required = false) Integer groupId) {
return authService.queryAuthGroups(modelId, groupId); return authService.queryAuthGroups(modelId, groupId);
} }
/** /** 新建权限组 */
* 新建权限组
*/
@PostMapping("/createGroup") @PostMapping("/createGroup")
public void newAuthGroup(@RequestBody AuthGroup group) { public void newAuthGroup(@RequestBody AuthGroup group) {
group.setGroupId(null); group.setGroupId(null);
@@ -68,9 +69,10 @@ public class AuthController {
* @return * @return
*/ */
@PostMapping("/queryAuthorizedRes") @PostMapping("/queryAuthorizedRes")
public AuthorizedResourceResp queryAuthorizedResources(@RequestBody QueryAuthResReq req, public AuthorizedResourceResp queryAuthorizedResources(
HttpServletRequest request, @RequestBody QueryAuthResReq req,
HttpServletResponse response) { HttpServletRequest request,
HttpServletResponse response) {
User user = UserHolder.findUser(request, response); User user = UserHolder.findUser(request, response);
return authService.queryAuthorizedResources(req, user); return authService.queryAuthorizedResources(req, user);
} }

View File

@@ -30,23 +30,27 @@ public class AuthServiceImpl implements AuthService {
private UserService userService; private UserService userService;
public AuthServiceImpl(JdbcTemplate jdbcTemplate, public AuthServiceImpl(JdbcTemplate jdbcTemplate, UserService userService) {
UserService userService) {
this.jdbcTemplate = jdbcTemplate; this.jdbcTemplate = jdbcTemplate;
this.userService = userService; this.userService = userService;
} }
private List<AuthGroup> load() { private List<AuthGroup> load() {
List<String> rows = jdbcTemplate.queryForList("select config from s2_auth_groups", String.class); List<String> rows =
jdbcTemplate.queryForList("select config from s2_auth_groups", String.class);
Gson g = new Gson(); Gson g = new Gson();
return rows.stream().map(row -> g.fromJson(row, AuthGroup.class)).collect(Collectors.toList()); return rows.stream()
.map(row -> g.fromJson(row, AuthGroup.class))
.collect(Collectors.toList());
} }
@Override @Override
public List<AuthGroup> queryAuthGroups(String modelId, Integer groupId) { public List<AuthGroup> queryAuthGroups(String modelId, Integer groupId) {
return load().stream() return load().stream()
.filter(group -> (Objects.isNull(groupId) || groupId.equals(group.getGroupId())) .filter(
&& modelId.equals(group.getModelId().toString())) group ->
(Objects.isNull(groupId) || groupId.equals(group.getGroupId()))
&& modelId.equals(group.getModelId().toString()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@@ -61,10 +65,14 @@ public class AuthServiceImpl implements AuthService {
nextGroupId = obj + 1; nextGroupId = obj + 1;
} }
group.setGroupId(nextGroupId); group.setGroupId(nextGroupId);
jdbcTemplate.update("insert into s2_auth_groups (group_id, config) values (?, ?);", nextGroupId, jdbcTemplate.update(
"insert into s2_auth_groups (group_id, config) values (?, ?);",
nextGroupId,
g.toJson(group)); g.toJson(group));
} else { } else {
jdbcTemplate.update("update s2_auth_groups set config = ? where group_id = ?;", g.toJson(group), jdbcTemplate.update(
"update s2_auth_groups set config = ? where group_id = ?;",
g.toJson(group),
group.getGroupId()); group.getGroupId());
} }
} }
@@ -80,10 +88,11 @@ public class AuthServiceImpl implements AuthService {
return new AuthorizedResourceResp(); return new AuthorizedResourceResp();
} }
Set<String> userOrgIds = userService.getUserAllOrgId(user.getName()); Set<String> userOrgIds = userService.getUserAllOrgId(user.getName());
List<AuthGroup> groups = getAuthGroups(req.getModelIds(), user.getName(), new ArrayList<>(userOrgIds)); List<AuthGroup> groups =
getAuthGroups(req.getModelIds(), user.getName(), new ArrayList<>(userOrgIds));
AuthorizedResourceResp resource = new AuthorizedResourceResp(); AuthorizedResourceResp resource = new AuthorizedResourceResp();
Map<Long, List<AuthGroup>> authGroupsByModelId = groups.stream() Map<Long, List<AuthGroup>> authGroupsByModelId =
.collect(Collectors.groupingBy(AuthGroup::getModelId)); groups.stream().collect(Collectors.groupingBy(AuthGroup::getModelId));
for (Long modelId : req.getModelIds()) { for (Long modelId : req.getModelIds()) {
if (authGroupsByModelId.containsKey(modelId)) { if (authGroupsByModelId.containsKey(modelId)) {
List<AuthGroup> authGroups = authGroupsByModelId.get(modelId); List<AuthGroup> authGroups = authGroupsByModelId.get(modelId);
@@ -110,26 +119,31 @@ public class AuthServiceImpl implements AuthService {
return resource; return resource;
} }
private List<AuthGroup> getAuthGroups(List<Long> modelIds, String userName, List<String> departmentIds) { private List<AuthGroup> getAuthGroups(
List<AuthGroup> groups = load().stream() List<Long> modelIds, String userName, List<String> departmentIds) {
.filter(group -> { List<AuthGroup> groups =
if (!modelIds.contains(group.getModelId())) { load().stream()
return false; .filter(
} group -> {
if (!CollectionUtils.isEmpty(group.getAuthorizedUsers()) if (!modelIds.contains(group.getModelId())) {
&& group.getAuthorizedUsers().contains(userName)) { return false;
return true; }
} if (!CollectionUtils.isEmpty(group.getAuthorizedUsers())
for (String departmentId : departmentIds) { && group.getAuthorizedUsers().contains(userName)) {
if (!CollectionUtils.isEmpty(group.getAuthorizedDepartmentIds()) return true;
&& group.getAuthorizedDepartmentIds().contains(departmentId)) { }
return true; for (String departmentId : departmentIds) {
} if (!CollectionUtils.isEmpty(
} group.getAuthorizedDepartmentIds())
return false; && group.getAuthorizedDepartmentIds()
}).collect(Collectors.toList()); .contains(departmentId)) {
return true;
}
}
return false;
})
.collect(Collectors.toList());
log.info("user:{} department:{} authGroups:{}", userName, departmentIds, groups); log.info("user:{} department:{} authGroups:{}", userName, departmentIds, groups);
return groups; return groups;
} }
} }

View File

@@ -1,11 +1,9 @@
package com.tencent.supersonic.chat.api.pojo.enums; package com.tencent.supersonic.chat.api.pojo.enums;
import com.tencent.supersonic.common.pojo.exception.InvalidArgumentException; import com.tencent.supersonic.common.pojo.exception.InvalidArgumentException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
public enum MemoryReviewResult { public enum MemoryReviewResult {
POSITIVE, POSITIVE,
NEGATIVE; NEGATIVE;

View File

@@ -1,10 +1,7 @@
package com.tencent.supersonic.chat.api.pojo.enums; package com.tencent.supersonic.chat.api.pojo.enums;
public enum MemoryStatus { public enum MemoryStatus {
PENDING, PENDING,
ENABLED, ENABLED,
DISABLED; DISABLED;
} }

View File

@@ -7,18 +7,13 @@ import java.util.List;
@Data @Data
public class ChatAggConfigReq { public class ChatAggConfigReq {
/** /** invisible dimensions/metrics */
* invisible dimensions/metrics
*/
private ItemVisibility visibility; private ItemVisibility visibility;
/** /** information about dictionary about the model */
* information about dictionary about the model
*/
private List<KnowledgeInfoReq> knowledgeInfos; private List<KnowledgeInfoReq> knowledgeInfos;
private KnowledgeAdvancedConfig globalKnowledgeConfig; private KnowledgeAdvancedConfig globalKnowledgeConfig;
private ChatDefaultConfigReq chatDefaultConfig; private ChatDefaultConfigReq chatDefaultConfig;
} }

View File

@@ -1,35 +1,24 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import com.tencent.supersonic.common.pojo.enums.StatusEnum; import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
import java.util.List; import java.util.List;
/** /** extended information command about model */
* extended information command about model
*/
@Data @Data
@ToString @ToString
public class ChatConfigBaseReq { public class ChatConfigBaseReq {
private Long modelId; private Long modelId;
/** the recommended questions about the model */
/**
* the recommended questions about the model
*/
private List<RecommendedQuestionReq> recommendedQuestions; private List<RecommendedQuestionReq> recommendedQuestions;
/** /** the llm examples about the model */
* the llm examples about the model
*/
private String llmExamples; private String llmExamples;
/** /** available status */
* available status
*/
private StatusEnum status; private StatusEnum status;
} }

View File

@@ -2,7 +2,6 @@ package com.tencent.supersonic.chat.api.pojo.request;
import lombok.Data; import lombok.Data;
@Data @Data
public class ChatConfigEditReqReq extends ChatConfigBaseReq { public class ChatConfigEditReqReq extends ChatConfigBaseReq {

View File

@@ -4,7 +4,6 @@ import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@Data @Data
public class ChatConfigFilter { public class ChatConfigFilter {

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import lombok.Data; import lombok.Data;
import java.util.ArrayList; import java.util.ArrayList;
@@ -11,6 +10,4 @@ public class ChatDefaultConfigReq {
private List<Long> dimensionIds = new ArrayList<>(); private List<Long> dimensionIds = new ArrayList<>();
private List<Long> metricIds = new ArrayList<>(); private List<Long> metricIds = new ArrayList<>();
} }

View File

@@ -7,18 +7,13 @@ import java.util.List;
@Data @Data
public class ChatDetailConfigReq { public class ChatDetailConfigReq {
/** /** invisible dimensions/metrics */
* invisible dimensions/metrics
*/
private ItemVisibility visibility; private ItemVisibility visibility;
/** /** information about dictionary about the model */
* information about dictionary about the model
*/
private List<KnowledgeInfoReq> knowledgeInfos; private List<KnowledgeInfoReq> knowledgeInfos;
private KnowledgeAdvancedConfig globalKnowledgeConfig; private KnowledgeAdvancedConfig globalKnowledgeConfig;
private ChatDefaultConfigReq chatDefaultConfig; private ChatDefaultConfigReq chatDefaultConfig;
} }

View File

@@ -18,5 +18,4 @@ public class ChatExecuteReq {
private int parseId; private int parseId;
private String queryText; private String queryText;
private boolean saveAnswer; private boolean saveAnswer;
} }

View File

@@ -26,5 +26,4 @@ public class ChatMemoryFilter {
private MemoryReviewResult llmReviewRet; private MemoryReviewResult llmReviewRet;
private MemoryReviewResult humanReviewRet; private MemoryReviewResult humanReviewRet;
} }

View File

@@ -1,13 +1,12 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import javax.validation.constraints.NotNull;
import com.tencent.supersonic.chat.api.pojo.enums.MemoryReviewResult; import com.tencent.supersonic.chat.api.pojo.enums.MemoryReviewResult;
import com.tencent.supersonic.chat.api.pojo.enums.MemoryStatus; import com.tencent.supersonic.chat.api.pojo.enums.MemoryStatus;
import com.tencent.supersonic.common.pojo.RecordInfo; import com.tencent.supersonic.common.pojo.RecordInfo;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotNull;
@Data @Data
public class ChatMemoryUpdateReq extends RecordInfo { public class ChatMemoryUpdateReq extends RecordInfo {
@@ -23,5 +22,4 @@ public class ChatMemoryUpdateReq extends RecordInfo {
private MemoryReviewResult humanReviewRet; private MemoryReviewResult humanReviewRet;
private String humanReviewCmt; private String humanReviewCmt;
} }

View File

@@ -15,5 +15,4 @@ public class ChatParseReq {
private QueryFilters queryFilters; private QueryFilters queryFilters;
private boolean saveAnswer = true; private boolean saveAnswer = true;
private SchemaMapInfo mapInfo = new SchemaMapInfo(); private SchemaMapInfo mapInfo = new SchemaMapInfo();
} }

View File

@@ -1,29 +1,22 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import java.util.List;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
/** import java.util.List;
* the entity info about the model
*/ /** the entity info about the model */
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@ToString @ToString
@NoArgsConstructor @NoArgsConstructor
public class Entity { public class Entity {
/** /** uniquely identifies an entity */
* uniquely identifies an entity
*/
private Long entityId; private Long entityId;
/** /** entity name list */
* entity name list
*/
private List<String> names; private List<String> names;
} }

View File

@@ -6,18 +6,13 @@ import lombok.ToString;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Data @Data
@ToString @ToString
public class ItemNameVisibilityInfo { public class ItemNameVisibilityInfo {
/** /** invisible dimensions */
* invisible dimensions
*/
private List<String> blackDimNameList = new ArrayList<>(); private List<String> blackDimNameList = new ArrayList<>();
/** /** invisible metrics */
* invisible metrics
*/
private List<String> blackMetricNameList = new ArrayList<>(); private List<String> blackMetricNameList = new ArrayList<>();
} }

View File

@@ -1,22 +1,18 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import java.util.ArrayList;
import java.util.List;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
import java.util.ArrayList;
import java.util.List;
@Data @Data
@ToString @ToString
public class ItemVisibility { public class ItemVisibility {
/** /** invisible dimensions */
* invisible dimensions
*/
private List<Long> blackDimIdList = new ArrayList<>(); private List<Long> blackDimIdList = new ArrayList<>();
/** /** invisible metrics */
* invisible metrics
*/
private List<Long> blackMetricIdList = new ArrayList<>(); private List<Long> blackMetricIdList = new ArrayList<>();
} }

View File

@@ -5,9 +5,7 @@ import lombok.Data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /** advanced knowledge config */
* advanced knowledge config
*/
@Data @Data
public class KnowledgeAdvancedConfig { public class KnowledgeAdvancedConfig {

View File

@@ -1,35 +1,23 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import com.tencent.supersonic.common.pojo.enums.TypeEnums;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import com.tencent.supersonic.common.pojo.enums.TypeEnums;
import lombok.Data; import lombok.Data;
/** /** information about dictionary about the model */
* information about dictionary about the model
*/
@Data @Data
public class KnowledgeInfoReq { public class KnowledgeInfoReq {
/** /** metricId、DimensionId、modelId */
* metricId、DimensionId、modelId
*/
private Long itemId; private Long itemId;
private String bizName; private String bizName;
/** /** type: IntentionTypeEnum temporarily only supports dimension-related information */
* type: IntentionTypeEnum @NotNull private TypeEnums type = TypeEnums.DIMENSION;
* temporarily only supports dimension-related information
*/
@NotNull
private TypeEnums type = TypeEnums.DIMENSION;
private Boolean searchEnable = false; private Boolean searchEnable = false;
/** /** advanced knowledge config for single item */
* advanced knowledge config for single item
*/
private KnowledgeAdvancedConfig knowledgeAdvancedConfig; private KnowledgeAdvancedConfig knowledgeAdvancedConfig;
} }

View File

@@ -3,10 +3,8 @@ package com.tencent.supersonic.chat.api.pojo.request;
import com.tencent.supersonic.common.pojo.PageBaseReq; import com.tencent.supersonic.common.pojo.PageBaseReq;
import lombok.Data; import lombok.Data;
@Data @Data
public class PageMemoryReq extends PageBaseReq { public class PageMemoryReq extends PageBaseReq {
private ChatMemoryFilter chatMemoryFilter = new ChatMemoryFilter(); private ChatMemoryFilter chatMemoryFilter = new ChatMemoryFilter();
} }

View File

@@ -1,6 +1,7 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import lombok.Data; import lombok.Data;
import java.util.List; import java.util.List;
@Data @Data

View File

@@ -1,12 +1,10 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import lombok.Data; import lombok.Data;
@Data @Data
public class PluginQueryReq { public class PluginQueryReq {
private String name; private String name;
private String parseMode; private String parseMode;

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import lombok.Data; import lombok.Data;
@Data @Data
@@ -9,5 +8,4 @@ public class RecommendReq {
private Long modelId; private Long modelId;
private Long metricId; private Long metricId;
} }

View File

@@ -12,5 +12,4 @@ import lombok.ToString;
public class RecommendedQuestionReq { public class RecommendedQuestionReq {
private String question; private String question;
} }

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.api.pojo.request; package com.tencent.supersonic.chat.api.pojo.request;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@@ -21,5 +20,4 @@ public class SimilarQueryReq {
private Long dataSetId; private Long dataSetId;
private Integer agentId; private Integer agentId;
} }

View File

@@ -9,18 +9,13 @@ import java.util.List;
@Data @Data
public class ChatAggRichConfigResp { public class ChatAggRichConfigResp {
/** /** invisible dimensions/metrics */
* invisible dimensions/metrics
*/
private ItemVisibilityInfo visibility; private ItemVisibilityInfo visibility;
/** /** information about dictionary about the model */
* information about dictionary about the model
*/
private List<KnowledgeInfoReq> knowledgeInfos; private List<KnowledgeInfoReq> knowledgeInfos;
private KnowledgeAdvancedConfig globalKnowledgeConfig; private KnowledgeAdvancedConfig globalKnowledgeConfig;
private ChatDefaultRichConfigResp chatDefaultConfig; private ChatDefaultRichConfigResp chatDefaultConfig;
} }

View File

@@ -4,12 +4,11 @@ import com.tencent.supersonic.chat.api.pojo.request.ChatAggConfigReq;
import com.tencent.supersonic.chat.api.pojo.request.ChatDetailConfigReq; import com.tencent.supersonic.chat.api.pojo.request.ChatDetailConfigReq;
import com.tencent.supersonic.chat.api.pojo.request.RecommendedQuestionReq; import com.tencent.supersonic.chat.api.pojo.request.RecommendedQuestionReq;
import com.tencent.supersonic.common.pojo.enums.StatusEnum; import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import lombok.Data;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import lombok.Data;
@Data @Data
public class ChatConfigResp { public class ChatConfigResp {
@@ -25,9 +24,7 @@ public class ChatConfigResp {
private String llmExamples; private String llmExamples;
/** /** available status */
* available status
*/
private StatusEnum statusEnum; private StatusEnum statusEnum;
private String createdBy; private String createdBy;

View File

@@ -2,11 +2,11 @@ package com.tencent.supersonic.chat.api.pojo.response;
import com.tencent.supersonic.chat.api.pojo.request.RecommendedQuestionReq; import com.tencent.supersonic.chat.api.pojo.request.RecommendedQuestionReq;
import com.tencent.supersonic.common.pojo.enums.StatusEnum; import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import lombok.Data;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import lombok.Data;
@Data @Data
public class ChatConfigRichResp { public class ChatConfigRichResp {
@@ -23,9 +23,7 @@ public class ChatConfigRichResp {
private List<RecommendedQuestionReq> recommendedQuestions; private List<RecommendedQuestionReq> recommendedQuestions;
/** /** available status */
* available status
*/
private StatusEnum statusEnum; private StatusEnum statusEnum;
private String createdBy; private String createdBy;

View File

@@ -1,9 +1,8 @@
package com.tencent.supersonic.chat.api.pojo.response; package com.tencent.supersonic.chat.api.pojo.response;
import com.tencent.supersonic.headless.api.pojo.SchemaElement;
import com.tencent.supersonic.common.pojo.Constants; import com.tencent.supersonic.common.pojo.Constants;
import com.tencent.supersonic.common.pojo.enums.TimeMode; import com.tencent.supersonic.common.pojo.enums.TimeMode;
import com.tencent.supersonic.headless.api.pojo.SchemaElement;
import lombok.Data; import lombok.Data;
import java.util.List; import java.util.List;
@@ -14,18 +13,11 @@ public class ChatDefaultRichConfigResp {
private List<SchemaElement> dimensions; private List<SchemaElement> dimensions;
private List<SchemaElement> metrics; private List<SchemaElement> metrics;
/** default time span unit */
/**
* default time span unit
*/
private Integer unit = 1; private Integer unit = 1;
/** /** default time type: DAY, WEEK, MONTH, YEAR */
* default time type:
* DAY, WEEK, MONTH, YEAR
*/
private String period = Constants.DAY; private String period = Constants.DAY;
private TimeMode timeMode; private TimeMode timeMode;
} }

View File

@@ -9,19 +9,13 @@ import java.util.List;
@Data @Data
public class ChatDetailRichConfigResp { public class ChatDetailRichConfigResp {
/** /** invisible dimensions/metrics */
* invisible dimensions/metrics
*/
private ItemVisibilityInfo visibility; private ItemVisibilityInfo visibility;
/** /** information about dictionary about the model */
* information about dictionary about the model
*/
private List<KnowledgeInfoReq> knowledgeInfos; private List<KnowledgeInfoReq> knowledgeInfos;
private KnowledgeAdvancedConfig globalKnowledgeConfig; private KnowledgeAdvancedConfig globalKnowledgeConfig;
private ChatDefaultRichConfigResp chatDefaultConfig; private ChatDefaultRichConfigResp chatDefaultConfig;
} }

View File

@@ -1,15 +1,13 @@
package com.tencent.supersonic.chat.api.pojo.response; package com.tencent.supersonic.chat.api.pojo.response;
import com.tencent.supersonic.headless.api.pojo.SchemaElement; import com.tencent.supersonic.headless.api.pojo.SchemaElement;
import lombok.Data;
import java.util.List; import java.util.List;
import lombok.Data;
@Data @Data
public class EntityRichInfoResp { public class EntityRichInfoResp {
/** /** entity alias */
* entity alias
*/
private List<String> names; private List<String> names;
private SchemaElement dimItem; private SchemaElement dimItem;

View File

@@ -1,8 +1,8 @@
package com.tencent.supersonic.chat.api.pojo.response; package com.tencent.supersonic.chat.api.pojo.response;
import java.util.List;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class ItemVisibilityInfo { public class ItemVisibilityInfo {

View File

@@ -3,10 +3,10 @@ package com.tencent.supersonic.chat.api.pojo.response;
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo; import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
import com.tencent.supersonic.headless.api.pojo.response.ParseTimeCostResp; import com.tencent.supersonic.headless.api.pojo.response.ParseTimeCostResp;
import lombok.Data; import lombok.Data;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@Data @Data
public class QueryResp { public class QueryResp {
@@ -20,5 +20,4 @@ public class QueryResp {
private List<SemanticParseInfo> parseInfos; private List<SemanticParseInfo> parseInfos;
private List<SimilarQueryRecallResp> similarQueries; private List<SimilarQueryRecallResp> similarQueries;
private ParseTimeCostResp parseTimeCost = new ParseTimeCostResp(); private ParseTimeCostResp parseTimeCost = new ParseTimeCostResp();
} }

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.api.pojo.response; package com.tencent.supersonic.chat.api.pojo.response;
import lombok.Data; import lombok.Data;
import java.util.List; import java.util.List;
@@ -14,5 +13,4 @@ public class ShowCaseResp {
private int pageSize; private int pageSize;
private int current; private int current;
} }

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.api.pojo.response; package com.tencent.supersonic.chat.api.pojo.response;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@@ -11,5 +10,4 @@ public class SimilarQueryRecallResp {
private Long queryId; private Long queryId;
private String queryText; private String queryText;
} }

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.server.agent; package com.tencent.supersonic.chat.server.agent;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@@ -28,10 +27,9 @@ public class Agent extends RecordInfo {
private String name; private String name;
private String description; private String description;
/** /** 0 offline, 1 online */
* 0 offline, 1 online
*/
private Integer status; private Integer status;
private List<String> examples; private List<String> examples;
private String agentConfig; private String agentConfig;
private ChatModelConfig modelConfig; private ChatModelConfig modelConfig;
@@ -46,13 +44,13 @@ public class Agent extends RecordInfo {
} }
List<Map> toolList = (List) map.get("tools"); List<Map> toolList = (List) map.get("tools");
return toolList.stream() return toolList.stream()
.filter(tool -> { .filter(
tool -> {
if (Objects.isNull(type)) { if (Objects.isNull(type)) {
return true; return true;
} }
return type.name().equals(tool.get("type")); return type.name().equals(tool.get("type"));
} })
)
.map(JSONObject::toJSONString) .map(JSONObject::toJSONString)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@@ -74,7 +72,8 @@ public class Agent extends RecordInfo {
if (CollectionUtils.isEmpty(tools)) { if (CollectionUtils.isEmpty(tools)) {
return Lists.newArrayList(); return Lists.newArrayList();
} }
return tools.stream().map(tool -> JSONObject.parseObject(tool, NL2SQLTool.class)) return tools.stream()
.map(tool -> JSONObject.parseObject(tool, NL2SQLTool.class))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@@ -121,7 +120,8 @@ public class Agent extends RecordInfo {
if (CollectionUtils.isEmpty(commonAgentTools)) { if (CollectionUtils.isEmpty(commonAgentTools)) {
return new HashSet<>(); return new HashSet<>();
} }
return commonAgentTools.stream().map(NL2SQLTool::getDataSetIds) return commonAgentTools.stream()
.map(NL2SQLTool::getDataSetIds)
.filter(modelIds -> !CollectionUtils.isEmpty(modelIds)) .filter(modelIds -> !CollectionUtils.isEmpty(modelIds))
.flatMap(Collection::stream) .flatMap(Collection::stream)
.collect(Collectors.toSet()); .collect(Collectors.toSet());

View File

@@ -4,8 +4,8 @@ import com.google.common.collect.Lists;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.util.List;
import java.util.List;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@@ -13,5 +13,4 @@ import java.util.List;
public class AgentConfig { public class AgentConfig {
List<AgentTool> tools = Lists.newArrayList(); List<AgentTool> tools = Lists.newArrayList();
} }

View File

@@ -21,5 +21,4 @@ public enum AgentToolType {
map.put(PLUGIN, PLUGIN.title); map.put(PLUGIN, PLUGIN.title);
return map; return map;
} }
} }

View File

@@ -8,5 +8,4 @@ import java.util.List;
public class LLMParserTool extends NL2SQLTool { public class LLMParserTool extends NL2SQLTool {
private List<String> exampleQuestions; private List<String> exampleQuestions;
} }

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.server.agent; package com.tencent.supersonic.chat.server.agent;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@@ -11,5 +10,4 @@ import lombok.NoArgsConstructor;
public class MultiTurnConfig { public class MultiTurnConfig {
private boolean enableMultiTurn; private boolean enableMultiTurn;
} }

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.server.agent; package com.tencent.supersonic.chat.server.agent;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@@ -13,5 +12,4 @@ import java.util.List;
public class NL2SQLTool extends AgentTool { public class NL2SQLTool extends AgentTool {
protected List<Long> dataSetIds; protected List<Long> dataSetIds;
} }

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.server.agent; package com.tencent.supersonic.chat.server.agent;
import lombok.Data; import lombok.Data;
import java.util.List; import java.util.List;
@@ -9,5 +8,4 @@ import java.util.List;
public class PluginTool extends AgentTool { public class PluginTool extends AgentTool {
private List<Long> plugins; private List<Long> plugins;
} }

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.server.agent; package com.tencent.supersonic.chat.server.agent;
import lombok.Data; import lombok.Data;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@@ -9,7 +8,6 @@ import java.util.List;
@Data @Data
public class RuleParserTool extends NL2SQLTool { public class RuleParserTool extends NL2SQLTool {
private List<String> queryModes; private List<String> queryModes;
private List<String> queryTypes; private List<String> queryTypes;
@@ -17,5 +15,4 @@ public class RuleParserTool extends NL2SQLTool {
public boolean isContainsAllModel() { public boolean isContainsAllModel() {
return CollectionUtils.isNotEmpty(dataSetIds) && dataSetIds.contains(-1L); return CollectionUtils.isNotEmpty(dataSetIds) && dataSetIds.contains(-1L);
} }
} }

View File

@@ -3,8 +3,8 @@ package com.tencent.supersonic.chat.server.config;
import com.tencent.supersonic.chat.api.pojo.request.ChatAggConfigReq; import com.tencent.supersonic.chat.api.pojo.request.ChatAggConfigReq;
import com.tencent.supersonic.chat.api.pojo.request.ChatDetailConfigReq; import com.tencent.supersonic.chat.api.pojo.request.ChatDetailConfigReq;
import com.tencent.supersonic.chat.api.pojo.request.RecommendedQuestionReq; import com.tencent.supersonic.chat.api.pojo.request.RecommendedQuestionReq;
import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import com.tencent.supersonic.common.pojo.RecordInfo; import com.tencent.supersonic.common.pojo.RecordInfo;
import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
@@ -14,33 +14,22 @@ import java.util.List;
@ToString @ToString
public class ChatConfig { public class ChatConfig {
/** /** database auto-increment primary key */
* database auto-increment primary key
*/
private Long id; private Long id;
private Long modelId; private Long modelId;
/** /** the chatDetailConfig about the model */
* the chatDetailConfig about the model
*/
private ChatDetailConfigReq chatDetailConfig; private ChatDetailConfigReq chatDetailConfig;
/** /** the chatAggConfig about the model */
* the chatAggConfig about the model
*/
private ChatAggConfigReq chatAggConfig; private ChatAggConfigReq chatAggConfig;
private List<RecommendedQuestionReq> recommendedQuestions; private List<RecommendedQuestionReq> recommendedQuestions;
/** /** available status */
* available status
*/
private StatusEnum status; private StatusEnum status;
/** /** about createdBy, createdAt, updatedBy, updatedAt */
* about createdBy, createdAt, updatedBy, updatedAt
*/
private RecordInfo recordInfo; private RecordInfo recordInfo;
} }

View File

@@ -1,10 +1,9 @@
package com.tencent.supersonic.chat.server.executor; package com.tencent.supersonic.chat.server.executor;
import com.tencent.supersonic.chat.server.pojo.ExecuteContext;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult; import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
import com.tencent.supersonic.chat.server.pojo.ExecuteContext;
public interface ChatQueryExecutor { public interface ChatQueryExecutor {
QueryResult execute(ExecuteContext executeContext); QueryResult execute(ExecuteContext executeContext);
} }

View File

@@ -1,6 +1,7 @@
package com.tencent.supersonic.chat.server.executor; package com.tencent.supersonic.chat.server.executor;
import com.tencent.supersonic.chat.api.pojo.response.QueryResp; import com.tencent.supersonic.chat.api.pojo.response.QueryResp;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
import com.tencent.supersonic.chat.server.agent.Agent; import com.tencent.supersonic.chat.server.agent.Agent;
import com.tencent.supersonic.chat.server.agent.MultiTurnConfig; import com.tencent.supersonic.chat.server.agent.MultiTurnConfig;
import com.tencent.supersonic.chat.server.parser.ParserConfig; import com.tencent.supersonic.chat.server.parser.ParserConfig;
@@ -8,7 +9,6 @@ import com.tencent.supersonic.chat.server.pojo.ExecuteContext;
import com.tencent.supersonic.chat.server.service.AgentService; import com.tencent.supersonic.chat.server.service.AgentService;
import com.tencent.supersonic.chat.server.service.ChatManageService; import com.tencent.supersonic.chat.server.service.ChatManageService;
import com.tencent.supersonic.common.util.ContextUtils; import com.tencent.supersonic.common.util.ContextUtils;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
import com.tencent.supersonic.headless.api.pojo.response.QueryState; import com.tencent.supersonic.headless.api.pojo.response.QueryState;
import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.chat.ChatLanguageModel;
@@ -26,13 +26,14 @@ import static com.tencent.supersonic.chat.server.parser.ParserConfig.PARSER_MULT
public class PlainTextExecutor implements ChatQueryExecutor { public class PlainTextExecutor implements ChatQueryExecutor {
private static final String INSTRUCTION = "" private static final String INSTRUCTION =
+ "#Role: You are a nice person to talk to.\n" ""
+ "#Task: Respond quickly and nicely to the user." + "#Role: You are a nice person to talk to.\n"
+ "#Rules: 1.ALWAYS use the same language as the input.\n" + "#Task: Respond quickly and nicely to the user."
+ "#History Inputs: %s\n" + "#Rules: 1.ALWAYS use the same language as the input.\n"
+ "#Current Input: %s\n" + "#History Inputs: %s\n"
+ "#Your response: "; + "#Current Input: %s\n"
+ "#Your response: ";
@Override @Override
public QueryResult execute(ExecuteContext executeContext) { public QueryResult execute(ExecuteContext executeContext) {
@@ -40,14 +41,18 @@ public class PlainTextExecutor implements ChatQueryExecutor {
return null; return null;
} }
String promptStr = String.format(INSTRUCTION, getHistoryInputs(executeContext), String promptStr =
executeContext.getQueryText()); String.format(
INSTRUCTION,
getHistoryInputs(executeContext),
executeContext.getQueryText());
Prompt prompt = PromptTemplate.from(promptStr).apply(Collections.EMPTY_MAP); Prompt prompt = PromptTemplate.from(promptStr).apply(Collections.EMPTY_MAP);
AgentService agentService = ContextUtils.getBean(AgentService.class); AgentService agentService = ContextUtils.getBean(AgentService.class);
Agent chatAgent = agentService.getAgent(executeContext.getAgent().getId()); Agent chatAgent = agentService.getAgent(executeContext.getAgent().getId());
ChatLanguageModel chatLanguageModel = ModelProvider.getChatModel(chatAgent.getModelConfig()); ChatLanguageModel chatLanguageModel =
ModelProvider.getChatModel(chatAgent.getModelConfig());
Response<AiMessage> response = chatLanguageModel.generate(prompt.toUserMessage()); Response<AiMessage> response = chatLanguageModel.generate(prompt.toUserMessage());
QueryResult result = new QueryResult(); QueryResult result = new QueryResult();
@@ -66,16 +71,21 @@ public class PlainTextExecutor implements ChatQueryExecutor {
ParserConfig parserConfig = ContextUtils.getBean(ParserConfig.class); ParserConfig parserConfig = ContextUtils.getBean(ParserConfig.class);
MultiTurnConfig agentMultiTurnConfig = chatAgent.getMultiTurnConfig(); MultiTurnConfig agentMultiTurnConfig = chatAgent.getMultiTurnConfig();
Boolean globalMultiTurnConfig = Boolean.valueOf(parserConfig.getParameterValue(PARSER_MULTI_TURN_ENABLE)); Boolean globalMultiTurnConfig =
Boolean multiTurnConfig = agentMultiTurnConfig != null Boolean.valueOf(parserConfig.getParameterValue(PARSER_MULTI_TURN_ENABLE));
? agentMultiTurnConfig.isEnableMultiTurn() : globalMultiTurnConfig; Boolean multiTurnConfig =
agentMultiTurnConfig != null
? agentMultiTurnConfig.isEnableMultiTurn()
: globalMultiTurnConfig;
if (Boolean.TRUE.equals(multiTurnConfig)) { if (Boolean.TRUE.equals(multiTurnConfig)) {
List<QueryResp> queryResps = getHistoryQueries(executeContext.getChatId(), 5); List<QueryResp> queryResps = getHistoryQueries(executeContext.getChatId(), 5);
queryResps.stream().forEach(p -> { queryResps.stream()
historyInput.append(p.getQueryText()); .forEach(
historyInput.append(";"); p -> {
}); historyInput.append(p.getQueryText());
historyInput.append(";");
});
} }
return historyInput.toString(); return historyInput.toString();
@@ -83,17 +93,20 @@ public class PlainTextExecutor implements ChatQueryExecutor {
private List<QueryResp> getHistoryQueries(int chatId, int multiNum) { private List<QueryResp> getHistoryQueries(int chatId, int multiNum) {
ChatManageService chatManageService = ContextUtils.getBean(ChatManageService.class); ChatManageService chatManageService = ContextUtils.getBean(ChatManageService.class);
List<QueryResp> contextualParseInfoList = chatManageService.getChatQueries(chatId) List<QueryResp> contextualParseInfoList =
.stream() chatManageService.getChatQueries(chatId).stream()
.filter(q -> Objects.nonNull(q.getQueryResult()) .filter(
&& q.getQueryResult().getQueryState() == QueryState.SUCCESS) q ->
.collect(Collectors.toList()); Objects.nonNull(q.getQueryResult())
&& q.getQueryResult().getQueryState()
== QueryState.SUCCESS)
.collect(Collectors.toList());
List<QueryResp> contextualList = contextualParseInfoList.subList(0, List<QueryResp> contextualList =
Math.min(multiNum, contextualParseInfoList.size())); contextualParseInfoList.subList(
0, Math.min(multiNum, contextualParseInfoList.size()));
Collections.reverse(contextualList); Collections.reverse(contextualList);
return contextualList; return contextualList;
} }
} }

View File

@@ -1,10 +1,10 @@
package com.tencent.supersonic.chat.server.executor; package com.tencent.supersonic.chat.server.executor;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
import com.tencent.supersonic.chat.server.plugin.PluginQueryManager; import com.tencent.supersonic.chat.server.plugin.PluginQueryManager;
import com.tencent.supersonic.chat.server.plugin.build.PluginSemanticQuery; import com.tencent.supersonic.chat.server.plugin.build.PluginSemanticQuery;
import com.tencent.supersonic.chat.server.pojo.ExecuteContext; import com.tencent.supersonic.chat.server.pojo.ExecuteContext;
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo; import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
public class PluginExecutor implements ChatQueryExecutor { public class PluginExecutor implements ChatQueryExecutor {
@@ -18,5 +18,4 @@ public class PluginExecutor implements ChatQueryExecutor {
query.setParseInfo(parseInfo); query.setParseInfo(parseInfo);
return query.build(); return query.build();
} }
} }

View File

@@ -1,8 +1,11 @@
package com.tencent.supersonic.chat.server.executor; package com.tencent.supersonic.chat.server.executor;
import com.tencent.supersonic.chat.api.pojo.enums.MemoryStatus; import com.tencent.supersonic.chat.api.pojo.enums.MemoryStatus;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatMemoryDO; import com.tencent.supersonic.chat.server.persistence.dataobject.ChatMemoryDO;
import com.tencent.supersonic.chat.server.pojo.ChatContext;
import com.tencent.supersonic.chat.server.pojo.ExecuteContext; import com.tencent.supersonic.chat.server.pojo.ExecuteContext;
import com.tencent.supersonic.chat.server.service.ChatContextService;
import com.tencent.supersonic.chat.server.service.MemoryService; import com.tencent.supersonic.chat.server.service.MemoryService;
import com.tencent.supersonic.chat.server.util.ResultFormatter; import com.tencent.supersonic.chat.server.util.ResultFormatter;
import com.tencent.supersonic.common.pojo.Text2SQLExemplar; import com.tencent.supersonic.common.pojo.Text2SQLExemplar;
@@ -10,13 +13,10 @@ import com.tencent.supersonic.common.util.ContextUtils;
import com.tencent.supersonic.common.util.JsonUtil; import com.tencent.supersonic.common.util.JsonUtil;
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo; import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
import com.tencent.supersonic.headless.api.pojo.request.QuerySqlReq; import com.tencent.supersonic.headless.api.pojo.request.QuerySqlReq;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
import com.tencent.supersonic.headless.api.pojo.response.QueryState; import com.tencent.supersonic.headless.api.pojo.response.QueryState;
import com.tencent.supersonic.headless.api.pojo.response.SemanticQueryResp; import com.tencent.supersonic.headless.api.pojo.response.SemanticQueryResp;
import com.tencent.supersonic.chat.server.pojo.ChatContext;
import com.tencent.supersonic.headless.chat.query.llm.s2sql.LLMSqlQuery; import com.tencent.supersonic.headless.chat.query.llm.s2sql.LLMSqlQuery;
import com.tencent.supersonic.headless.server.facade.service.SemanticLayerService; import com.tencent.supersonic.headless.server.facade.service.SemanticLayerService;
import com.tencent.supersonic.chat.server.service.ChatContextService;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -31,28 +31,35 @@ public class SqlExecutor implements ChatQueryExecutor {
QueryResult queryResult = doExecute(executeContext); QueryResult queryResult = doExecute(executeContext);
if (queryResult != null) { if (queryResult != null) {
String textResult = ResultFormatter.transform2TextNew(queryResult.getQueryColumns(), String textResult =
queryResult.getQueryResults()); ResultFormatter.transform2TextNew(
queryResult.getQueryColumns(), queryResult.getQueryResults());
queryResult.setTextResult(textResult); queryResult.setTextResult(textResult);
if (queryResult.getQueryState().equals(QueryState.SUCCESS) if (queryResult.getQueryState().equals(QueryState.SUCCESS)
&& queryResult.getQueryMode().equals(LLMSqlQuery.QUERY_MODE)) { && queryResult.getQueryMode().equals(LLMSqlQuery.QUERY_MODE)) {
Text2SQLExemplar exemplar = JsonUtil.toObject(JsonUtil.toString( Text2SQLExemplar exemplar =
executeContext.getParseInfo().getProperties() JsonUtil.toObject(
.get(Text2SQLExemplar.PROPERTY_KEY)), Text2SQLExemplar.class); JsonUtil.toString(
executeContext
.getParseInfo()
.getProperties()
.get(Text2SQLExemplar.PROPERTY_KEY)),
Text2SQLExemplar.class);
MemoryService memoryService = ContextUtils.getBean(MemoryService.class); MemoryService memoryService = ContextUtils.getBean(MemoryService.class);
memoryService.createMemory(ChatMemoryDO.builder() memoryService.createMemory(
.agentId(executeContext.getAgent().getId()) ChatMemoryDO.builder()
.status(MemoryStatus.PENDING) .agentId(executeContext.getAgent().getId())
.question(exemplar.getQuestion()) .status(MemoryStatus.PENDING)
.sideInfo(exemplar.getSideInfo()) .question(exemplar.getQuestion())
.dbSchema(exemplar.getDbSchema()) .sideInfo(exemplar.getSideInfo())
.s2sql(exemplar.getSql()) .dbSchema(exemplar.getDbSchema())
.createdBy(executeContext.getUser().getName()) .s2sql(exemplar.getSql())
.updatedBy(executeContext.getUser().getName()) .createdBy(executeContext.getUser().getName())
.createdAt(new Date()) .updatedBy(executeContext.getUser().getName())
.build()); .createdAt(new Date())
.build());
} }
} }
@@ -71,9 +78,8 @@ public class SqlExecutor implements ChatQueryExecutor {
return null; return null;
} }
QuerySqlReq sqlReq = QuerySqlReq.builder() QuerySqlReq sqlReq =
.sql(parseInfo.getSqlInfo().getCorrectedS2SQL()) QuerySqlReq.builder().sql(parseInfo.getSqlInfo().getCorrectedS2SQL()).build();
.build();
sqlReq.setSqlInfo(parseInfo.getSqlInfo()); sqlReq.setSqlInfo(parseInfo.getSqlInfo());
sqlReq.setDataSetId(parseInfo.getDataSetId()); sqlReq.setDataSetId(parseInfo.getDataSetId());
@@ -97,5 +103,4 @@ public class SqlExecutor implements ChatQueryExecutor {
} }
return queryResult; return queryResult;
} }
} }

View File

@@ -27,27 +27,26 @@ public class MemoryReviewTask {
private static final Logger keyPipelineLog = LoggerFactory.getLogger("keyPipeline"); private static final Logger keyPipelineLog = LoggerFactory.getLogger("keyPipeline");
private static final String INSTRUCTION = "" private static final String INSTRUCTION =
+ "#Role: You are a senior data engineer experienced in writing SQL.\n" ""
+ "#Task: Your will be provided with a user question and the SQL written by junior engineer," + "#Role: You are a senior data engineer experienced in writing SQL.\n"
+ "please take a review and give your opinion.\n" + "#Task: Your will be provided with a user question and the SQL written by junior engineer,"
+ "#Rules: " + "please take a review and give your opinion.\n"
+ "1.ALWAYS follow the output format: `opinion=(POSITIVE|NEGATIVE),comment=(your comment)`." + "#Rules: "
+ "2.ALWAYS recognize `数据日期` as the date field." + "1.ALWAYS follow the output format: `opinion=(POSITIVE|NEGATIVE),comment=(your comment)`."
+ "3.IGNORE `数据日期` if not expressed in the `Question`." + "2.ALWAYS recognize `数据日期` as the date field."
+ "#Question: %s\n" + "3.IGNORE `数据日期` if not expressed in the `Question`."
+ "#Schema: %s\n" + "#Question: %s\n"
+ "#SideInfo: %s\n" + "#Schema: %s\n"
+ "#SQL: %s\n" + "#SideInfo: %s\n"
+ "#Response: "; + "#SQL: %s\n"
+ "#Response: ";
private static final Pattern OUTPUT_PATTERN = Pattern.compile("opinion=(.*),.*comment=(.*)"); private static final Pattern OUTPUT_PATTERN = Pattern.compile("opinion=(.*),.*comment=(.*)");
@Autowired @Autowired private MemoryService memoryService;
private MemoryService memoryService;
@Autowired @Autowired private AgentService agentService;
private AgentService agentService;
@Scheduled(fixedDelay = 60 * 1000) @Scheduled(fixedDelay = 60 * 1000)
public void review() { public void review() {
@@ -68,7 +67,8 @@ public class MemoryReviewTask {
Prompt prompt = PromptTemplate.from(promptStr).apply(Collections.EMPTY_MAP); Prompt prompt = PromptTemplate.from(promptStr).apply(Collections.EMPTY_MAP);
keyPipelineLog.info("MemoryReviewTask reqPrompt:\n{}", promptStr); keyPipelineLog.info("MemoryReviewTask reqPrompt:\n{}", promptStr);
ChatLanguageModel chatLanguageModel = ModelProvider.getChatModel(chatAgent.getModelConfig()); ChatLanguageModel chatLanguageModel =
ModelProvider.getChatModel(chatAgent.getModelConfig());
if (Objects.nonNull(chatLanguageModel)) { if (Objects.nonNull(chatLanguageModel)) {
String response = chatLanguageModel.generate(prompt.toUserMessage()).content().text(); String response = chatLanguageModel.generate(prompt.toUserMessage()).content().text();
keyPipelineLog.info("MemoryReviewTask modelResp:\n{}", response); keyPipelineLog.info("MemoryReviewTask modelResp:\n{}", response);
@@ -79,7 +79,8 @@ public class MemoryReviewTask {
} }
private String createPromptString(ChatMemoryDO m) { private String createPromptString(ChatMemoryDO m) {
return String.format(INSTRUCTION, m.getQuestion(), m.getDbSchema(), m.getSideInfo(), m.getS2sql()); return String.format(
INSTRUCTION, m.getQuestion(), m.getDbSchema(), m.getSideInfo(), m.getS2sql());
} }
private void processResponse(String response, ChatMemoryDO m) { private void processResponse(String response, ChatMemoryDO m) {

View File

@@ -6,5 +6,4 @@ import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
public interface ChatQueryParser { public interface ChatQueryParser {
void parse(ParseContext parseContext, ParseResp parseResp); void parse(ParseContext parseContext, ParseResp parseResp);
} }

View File

@@ -6,12 +6,14 @@ import com.tencent.supersonic.chat.server.util.ComponentFactory;
import com.tencent.supersonic.common.util.JsonUtil; import com.tencent.supersonic.common.util.JsonUtil;
import com.tencent.supersonic.headless.api.pojo.response.ParseResp; import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.List; import java.util.List;
@Slf4j @Slf4j
public class NL2PluginParser implements ChatQueryParser { public class NL2PluginParser implements ChatQueryParser {
private final List<PluginRecognizer> pluginRecognizers = ComponentFactory.getPluginRecognizers(); private final List<PluginRecognizer> pluginRecognizers =
ComponentFactory.getPluginRecognizers();
@Override @Override
public void parse(ParseContext parseContext, ParseResp parseResp) { public void parse(ParseContext parseContext, ParseResp parseResp) {
@@ -19,11 +21,13 @@ public class NL2PluginParser implements ChatQueryParser {
return; return;
} }
pluginRecognizers.forEach(pluginRecognizer -> { pluginRecognizers.forEach(
pluginRecognizer.recognize(parseContext, parseResp); pluginRecognizer -> {
log.info("{} recallResult:{}", pluginRecognizer.getClass().getSimpleName(), pluginRecognizer.recognize(parseContext, parseResp);
JsonUtil.toString(parseResp)); log.info(
}); "{} recallResult:{}",
pluginRecognizer.getClass().getSimpleName(),
JsonUtil.toString(parseResp));
});
} }
} }

View File

@@ -3,6 +3,7 @@ package com.tencent.supersonic.chat.server.parser;
import com.tencent.supersonic.chat.api.pojo.response.QueryResp; import com.tencent.supersonic.chat.api.pojo.response.QueryResp;
import com.tencent.supersonic.chat.server.agent.MultiTurnConfig; import com.tencent.supersonic.chat.server.agent.MultiTurnConfig;
import com.tencent.supersonic.chat.server.plugin.PluginQueryManager; import com.tencent.supersonic.chat.server.plugin.PluginQueryManager;
import com.tencent.supersonic.chat.server.pojo.ChatContext;
import com.tencent.supersonic.chat.server.pojo.ParseContext; import com.tencent.supersonic.chat.server.pojo.ParseContext;
import com.tencent.supersonic.chat.server.service.ChatContextService; import com.tencent.supersonic.chat.server.service.ChatContextService;
import com.tencent.supersonic.chat.server.service.ChatManageService; import com.tencent.supersonic.chat.server.service.ChatManageService;
@@ -19,7 +20,6 @@ import com.tencent.supersonic.headless.api.pojo.request.QueryFilter;
import com.tencent.supersonic.headless.api.pojo.request.QueryNLReq; import com.tencent.supersonic.headless.api.pojo.request.QueryNLReq;
import com.tencent.supersonic.headless.api.pojo.response.MapResp; import com.tencent.supersonic.headless.api.pojo.response.MapResp;
import com.tencent.supersonic.headless.api.pojo.response.ParseResp; import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
import com.tencent.supersonic.chat.server.pojo.ChatContext;
import com.tencent.supersonic.headless.api.pojo.response.QueryState; import com.tencent.supersonic.headless.api.pojo.response.QueryState;
import com.tencent.supersonic.headless.server.facade.service.ChatLayerService; import com.tencent.supersonic.headless.server.facade.service.ChatLayerService;
import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.data.message.AiMessage;
@@ -50,31 +50,33 @@ public class NL2SQLParser implements ChatQueryParser {
private static final Logger keyPipelineLog = LoggerFactory.getLogger("keyPipeline"); private static final Logger keyPipelineLog = LoggerFactory.getLogger("keyPipeline");
private static final String REWRITE_USER_QUESTION_INSTRUCTION = "" private static final String REWRITE_USER_QUESTION_INSTRUCTION =
+ "#Role: You are a data product manager experienced in data requirements." ""
+ "#Task: Your will be provided with current and history questions asked by a user," + "#Role: You are a data product manager experienced in data requirements."
+ "along with their mapped schema elements(metric, dimension and value)," + "#Task: Your will be provided with current and history questions asked by a user,"
+ "please try understanding the semantics and rewrite a question." + "along with their mapped schema elements(metric, dimension and value),"
+ "#Rules: " + "please try understanding the semantics and rewrite a question."
+ "1.ALWAYS keep relevant entities, metrics, dimensions, values and date ranges." + "#Rules: "
+ "2.ONLY respond with the rewritten question." + "1.ALWAYS keep relevant entities, metrics, dimensions, values and date ranges."
+ "#Current Question: {{current_question}}" + "2.ONLY respond with the rewritten question."
+ "#Current Mapped Schema: {{current_schema}}" + "#Current Question: {{current_question}}"
+ "#History Question: {{history_question}}" + "#Current Mapped Schema: {{current_schema}}"
+ "#History Mapped Schema: {{history_schema}}" + "#History Question: {{history_question}}"
+ "#History SQL: {{history_sql}}" + "#History Mapped Schema: {{history_schema}}"
+ "#Rewritten Question: "; + "#History SQL: {{history_sql}}"
+ "#Rewritten Question: ";
private static final String REWRITE_ERROR_MESSAGE_INSTRUCTION = "" private static final String REWRITE_ERROR_MESSAGE_INSTRUCTION =
+ "#Role: You are a data business partner who closely interacts with business people.\n" ""
+ "#Task: Your will be provided with user input, system output and some examples, " + "#Role: You are a data business partner who closely interacts with business people.\n"
+ "please respond shortly to teach user how to ask the right question, " + "#Task: Your will be provided with user input, system output and some examples, "
+ "by using `Examples` as references." + "please respond shortly to teach user how to ask the right question, "
+ "#Rules: ALWAYS respond with the same language as the `Input`.\n" + "by using `Examples` as references."
+ "#Input: {{user_question}}\n" + "#Rules: ALWAYS respond with the same language as the `Input`.\n"
+ "#Output: {{system_message}}\n" + "#Input: {{user_question}}\n"
+ "#Examples: {{examples}}\n" + "#Output: {{system_message}}\n"
+ "#Response: "; + "#Examples: {{examples}}\n"
+ "#Response: ";
@Override @Override
public void parse(ParseContext parseContext, ParseResp parseResp) { public void parse(ParseContext parseContext, ParseResp parseResp) {
@@ -84,8 +86,8 @@ public class NL2SQLParser implements ChatQueryParser {
ChatContextService chatContextService = ContextUtils.getBean(ChatContextService.class); ChatContextService chatContextService = ContextUtils.getBean(ChatContextService.class);
ChatContext chatCtx = chatContextService.getOrCreateContext(parseContext.getChatId()); ChatContext chatCtx = chatContextService.getOrCreateContext(parseContext.getChatId());
ChatLanguageModel chatLanguageModel = ModelProvider.getChatModel( ChatLanguageModel chatLanguageModel =
parseContext.getAgent().getModelConfig()); ModelProvider.getChatModel(parseContext.getAgent().getModelConfig());
processMultiTurn(chatLanguageModel, parseContext); processMultiTurn(chatLanguageModel, parseContext);
QueryNLReq queryNLReq = QueryReqConverter.buildText2SqlQueryReq(parseContext, chatCtx); QueryNLReq queryNLReq = QueryReqConverter.buildText2SqlQueryReq(parseContext, chatCtx);
@@ -96,11 +98,13 @@ public class NL2SQLParser implements ChatQueryParser {
if (ParseResp.ParseState.COMPLETED.equals(text2SqlParseResp.getState())) { if (ParseResp.ParseState.COMPLETED.equals(text2SqlParseResp.getState())) {
parseResp.getSelectedParses().addAll(text2SqlParseResp.getSelectedParses()); parseResp.getSelectedParses().addAll(text2SqlParseResp.getSelectedParses());
} else { } else {
parseResp.setErrorMsg(rewriteErrorMessage(chatLanguageModel, parseResp.setErrorMsg(
parseContext.getQueryText(), rewriteErrorMessage(
text2SqlParseResp.getErrorMsg(), chatLanguageModel,
queryNLReq.getDynamicExemplars(), parseContext.getQueryText(),
parseContext.getAgent().getExamples())); text2SqlParseResp.getErrorMsg(),
queryNLReq.getDynamicExemplars(),
parseContext.getAgent().getExamples()));
} }
parseResp.setState(text2SqlParseResp.getState()); parseResp.setState(text2SqlParseResp.getState());
parseResp.getParseTimeCost().setSqlTime(text2SqlParseResp.getParseTimeCost().getSqlTime()); parseResp.getParseTimeCost().setSqlTime(text2SqlParseResp.getParseTimeCost().getSqlTime());
@@ -134,24 +138,35 @@ public class NL2SQLParser implements ChatQueryParser {
StringBuilder textBuilder = new StringBuilder(); StringBuilder textBuilder = new StringBuilder();
textBuilder.append("**数据集:** ").append(parseInfo.getDataSet().getName()).append(" "); textBuilder.append("**数据集:** ").append(parseInfo.getDataSet().getName()).append(" ");
Optional<SchemaElement> metric = parseInfo.getMetrics().stream().findFirst(); Optional<SchemaElement> metric = parseInfo.getMetrics().stream().findFirst();
metric.ifPresent(schemaElement -> metric.ifPresent(
textBuilder.append("**指标:** ").append(schemaElement.getName()).append(" ")); schemaElement ->
List<String> dimensionNames = parseInfo.getDimensions().stream() textBuilder.append("**指标:** ").append(schemaElement.getName()).append(" "));
.map(SchemaElement::getName).filter(Objects::nonNull).collect(Collectors.toList()); List<String> dimensionNames =
parseInfo.getDimensions().stream()
.map(SchemaElement::getName)
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(dimensionNames)) { if (!CollectionUtils.isEmpty(dimensionNames)) {
textBuilder.append("**维度:** ").append(String.join(",", dimensionNames)); textBuilder.append("**维度:** ").append(String.join(",", dimensionNames));
} }
textBuilder.append("\n\n**筛选条件:** \n"); textBuilder.append("\n\n**筛选条件:** \n");
if (parseInfo.getDateInfo() != null) { if (parseInfo.getDateInfo() != null) {
textBuilder.append("**数据时间:** ").append(parseInfo.getDateInfo().getStartDate()).append("~") textBuilder
.append(parseInfo.getDateInfo().getEndDate()).append(" "); .append("**数据时间:** ")
.append(parseInfo.getDateInfo().getStartDate())
.append("~")
.append(parseInfo.getDateInfo().getEndDate())
.append(" ");
} }
if (!CollectionUtils.isEmpty(parseInfo.getDimensionFilters()) if (!CollectionUtils.isEmpty(parseInfo.getDimensionFilters())
|| CollectionUtils.isEmpty(parseInfo.getMetricFilters())) { || CollectionUtils.isEmpty(parseInfo.getMetricFilters())) {
Set<QueryFilter> queryFilters = parseInfo.getDimensionFilters(); Set<QueryFilter> queryFilters = parseInfo.getDimensionFilters();
queryFilters.addAll(parseInfo.getMetricFilters()); queryFilters.addAll(parseInfo.getMetricFilters());
for (QueryFilter queryFilter : queryFilters) { for (QueryFilter queryFilter : queryFilters) {
textBuilder.append("**").append(queryFilter.getName()).append("**") textBuilder
.append("**")
.append(queryFilter.getName())
.append("**")
.append(" ") .append(" ")
.append(queryFilter.getOperator().getValue()) .append(queryFilter.getOperator().getValue())
.append(" ") .append(" ")
@@ -165,10 +180,13 @@ public class NL2SQLParser implements ChatQueryParser {
private void processMultiTurn(ChatLanguageModel chatLanguageModel, ParseContext parseContext) { private void processMultiTurn(ChatLanguageModel chatLanguageModel, ParseContext parseContext) {
ParserConfig parserConfig = ContextUtils.getBean(ParserConfig.class); ParserConfig parserConfig = ContextUtils.getBean(ParserConfig.class);
MultiTurnConfig agentMultiTurnConfig = parseContext.getAgent().getMultiTurnConfig(); MultiTurnConfig agentMultiTurnConfig = parseContext.getAgent().getMultiTurnConfig();
Boolean globalMultiTurnConfig = Boolean.valueOf(parserConfig.getParameterValue(PARSER_MULTI_TURN_ENABLE)); Boolean globalMultiTurnConfig =
Boolean.valueOf(parserConfig.getParameterValue(PARSER_MULTI_TURN_ENABLE));
Boolean multiTurnConfig = agentMultiTurnConfig != null Boolean multiTurnConfig =
? agentMultiTurnConfig.isEnableMultiTurn() : globalMultiTurnConfig; agentMultiTurnConfig != null
? agentMultiTurnConfig.isEnableMultiTurn()
: globalMultiTurnConfig;
if (!Boolean.TRUE.equals(multiTurnConfig)) { if (!Boolean.TRUE.equals(multiTurnConfig)) {
return; return;
} }
@@ -186,7 +204,8 @@ public class NL2SQLParser implements ChatQueryParser {
SemanticParseInfo lastParseInfo = lastQuery.getParseInfos().get(0); SemanticParseInfo lastParseInfo = lastQuery.getParseInfos().get(0);
Long dataId = lastParseInfo.getDataSetId(); Long dataId = lastParseInfo.getDataSetId();
String curtMapStr = generateSchemaPrompt(currentMapResult.getMapInfo().getMatchedElements(dataId)); String curtMapStr =
generateSchemaPrompt(currentMapResult.getMapInfo().getMatchedElements(dataId));
String histMapStr = generateSchemaPrompt(lastParseInfo.getElementMatches()); String histMapStr = generateSchemaPrompt(lastParseInfo.getElementMatches());
String histSQL = lastParseInfo.getSqlInfo().getCorrectedS2SQL(); String histSQL = lastParseInfo.getSqlInfo().getCorrectedS2SQL();
@@ -207,22 +226,31 @@ public class NL2SQLParser implements ChatQueryParser {
QueryNLReq rewrittenQueryNLReq = QueryReqConverter.buildText2SqlQueryReq(parseContext); QueryNLReq rewrittenQueryNLReq = QueryReqConverter.buildText2SqlQueryReq(parseContext);
MapResp rewrittenQueryMapResult = chatLayerService.performMapping(rewrittenQueryNLReq); MapResp rewrittenQueryMapResult = chatLayerService.performMapping(rewrittenQueryNLReq);
parseContext.setMapInfo(rewrittenQueryMapResult.getMapInfo()); parseContext.setMapInfo(rewrittenQueryMapResult.getMapInfo());
log.info("Last Query: {} Current Query: {}, Rewritten Query: {}", log.info(
lastQuery.getQueryText(), currentMapResult.getQueryText(), rewrittenQuery); "Last Query: {} Current Query: {}, Rewritten Query: {}",
lastQuery.getQueryText(),
currentMapResult.getQueryText(),
rewrittenQuery);
} }
private String rewriteErrorMessage(ChatLanguageModel chatLanguageModel, String userQuestion, private String rewriteErrorMessage(
String errMsg, List<Text2SQLExemplar> similarExemplars, ChatLanguageModel chatLanguageModel,
List<String> agentExamples) { String userQuestion,
String errMsg,
List<Text2SQLExemplar> similarExemplars,
List<String> agentExamples) {
Map<String, Object> variables = new HashMap<>(); Map<String, Object> variables = new HashMap<>();
variables.put("user_question", userQuestion); variables.put("user_question", userQuestion);
variables.put("system_message", errMsg); variables.put("system_message", errMsg);
StringBuilder exampleStr = new StringBuilder(); StringBuilder exampleStr = new StringBuilder();
similarExemplars.forEach(e -> similarExemplars.forEach(
exampleStr.append(String.format("<Question:{%s},Schema:{%s}> ", e.getQuestion(), e.getDbSchema()))); e ->
agentExamples.forEach(e -> exampleStr.append(
exampleStr.append(String.format("<Question:{%s}> ", e))); String.format(
"<Question:{%s},Schema:{%s}> ",
e.getQuestion(), e.getDbSchema())));
agentExamples.forEach(e -> exampleStr.append(String.format("<Question:{%s}> ", e)));
variables.put("examples", exampleStr); variables.put("examples", exampleStr);
Prompt prompt = PromptTemplate.from(REWRITE_ERROR_MESSAGE_INSTRUCTION).apply(variables); Prompt prompt = PromptTemplate.from(REWRITE_ERROR_MESSAGE_INSTRUCTION).apply(variables);
@@ -262,14 +290,18 @@ public class NL2SQLParser implements ChatQueryParser {
private List<QueryResp> getHistoryQueries(int chatId, int multiNum) { private List<QueryResp> getHistoryQueries(int chatId, int multiNum) {
ChatManageService chatManageService = ContextUtils.getBean(ChatManageService.class); ChatManageService chatManageService = ContextUtils.getBean(ChatManageService.class);
List<QueryResp> contextualParseInfoList = chatManageService.getChatQueries(chatId) List<QueryResp> contextualParseInfoList =
.stream() chatManageService.getChatQueries(chatId).stream()
.filter(q -> Objects.nonNull(q.getQueryResult()) .filter(
&& q.getQueryResult().getQueryState() == QueryState.SUCCESS) q ->
.collect(Collectors.toList()); Objects.nonNull(q.getQueryResult())
&& q.getQueryResult().getQueryState()
== QueryState.SUCCESS)
.collect(Collectors.toList());
List<QueryResp> contextualList = contextualParseInfoList.subList(0, List<QueryResp> contextualList =
Math.min(multiNum, contextualParseInfoList.size())); contextualParseInfoList.subList(
0, Math.min(multiNum, contextualParseInfoList.size()));
Collections.reverse(contextualList); Collections.reverse(contextualList);
return contextualList; return contextualList;
} }
@@ -278,9 +310,8 @@ public class NL2SQLParser implements ChatQueryParser {
ExemplarServiceImpl exemplarManager = ContextUtils.getBean(ExemplarServiceImpl.class); ExemplarServiceImpl exemplarManager = ContextUtils.getBean(ExemplarServiceImpl.class);
EmbeddingConfig embeddingConfig = ContextUtils.getBean(EmbeddingConfig.class); EmbeddingConfig embeddingConfig = ContextUtils.getBean(EmbeddingConfig.class);
String memoryCollectionName = embeddingConfig.getMemoryCollectionName(agentId); String memoryCollectionName = embeddingConfig.getMemoryCollectionName(agentId);
List<Text2SQLExemplar> exemplars = exemplarManager.recallExemplars(memoryCollectionName, List<Text2SQLExemplar> exemplars =
queryNLReq.getQueryText(), 5); exemplarManager.recallExemplars(memoryCollectionName, queryNLReq.getQueryText(), 5);
queryNLReq.getDynamicExemplars().addAll(exemplars); queryNLReq.getDynamicExemplars().addAll(exemplars);
} }
} }

View File

@@ -10,8 +10,11 @@ import org.springframework.stereotype.Service;
public class ParserConfig extends ParameterConfig { public class ParserConfig extends ParameterConfig {
public static final Parameter PARSER_MULTI_TURN_ENABLE = public static final Parameter PARSER_MULTI_TURN_ENABLE =
new Parameter("s2.parser.multi-turn.enable", "false", new Parameter(
"是否开启多轮对话", "开启多轮对话将消耗更多token", "s2.parser.multi-turn.enable",
"bool", "Parser相关配置"); "false",
"是否开启多轮对话",
"开启多轮对话将消耗更多token",
"bool",
"Parser相关配置");
} }

View File

@@ -4,7 +4,6 @@ import com.tencent.supersonic.chat.server.pojo.ParseContext;
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo; import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
import com.tencent.supersonic.headless.api.pojo.response.ParseResp; import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
public class PlainTextParser implements ChatQueryParser { public class PlainTextParser implements ChatQueryParser {
@Override @Override
@@ -18,5 +17,4 @@ public class PlainTextParser implements ChatQueryParser {
parseResp.getSelectedParses().add(parseInfo); parseResp.getSelectedParses().add(parseInfo);
parseResp.setState(ParseResp.ParseState.COMPLETED); parseResp.setState(ParseResp.ParseState.COMPLETED);
} }
} }

View File

@@ -10,61 +10,40 @@ import java.util.Date;
@Data @Data
@TableName("s2_agent") @TableName("s2_agent")
public class AgentDO { public class AgentDO {
/** /** */
*
*/
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Integer id; private Integer id;
/** /** */
*
*/
private String name; private String name;
/** /** */
*
*/
private String description; private String description;
/** /** 0 offline, 1 online */
* 0 offline, 1 online
*/
private Integer status; private Integer status;
/** /** */
*
*/
private String examples; private String examples;
/** /** */
*
*/
private String config; private String config;
/** /** */
*
*/
private String createdBy; private String createdBy;
/** /** */
*
*/
private Date createdAt; private Date createdAt;
/** /** */
*
*/
private String updatedBy; private String updatedBy;
/** /** */
*
*/
private Date updatedAt; private Date updatedAt;
/** /** */
*
*/
private Integer enableSearch; private Integer enableSearch;
private Integer enableMemoryReview; private Integer enableMemoryReview;
private String modelConfig; private String modelConfig;
private String multiTurnConfig; private String multiTurnConfig;
@@ -72,5 +51,4 @@ public class AgentDO {
private String visualConfig; private String visualConfig;
private String promptConfig; private String promptConfig;
} }

View File

@@ -1,18 +1,15 @@
package com.tencent.supersonic.chat.server.persistence.dataobject; package com.tencent.supersonic.chat.server.persistence.dataobject;
import java.util.Date;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
import java.util.Date;
@Data @Data
@ToString @ToString
public class ChatConfigDO { public class ChatConfigDO {
/** /** database auto-increment primary key */
* database auto-increment primary key
*/
private Long id; private Long id;
private Long modelId; private Long modelId;
@@ -27,12 +24,10 @@ public class ChatConfigDO {
private String llmExamples; private String llmExamples;
/** /** record info */
* record info
*/
private String createdBy; private String createdBy;
private String updatedBy; private String updatedBy;
private Date createdAt; private Date createdAt;
private Date updatedAt; private Date updatedAt;
} }

View File

@@ -61,5 +61,4 @@ public class ChatMemoryDO {
@TableField("updated_at") @TableField("updated_at")
private Date updatedAt; private Date updatedAt;
} }

View File

@@ -4,54 +4,44 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
import java.util.Date; import java.util.Date;
@Data @Data
@TableName("s2_chat_query") @TableName("s2_chat_query")
public class ChatQueryDO { public class ChatQueryDO {
/** /** */
*/
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Long questionId; private Long questionId;
/** /** */
*/
private Integer agentId; private Integer agentId;
/** /** */
*/
private Date createTime; private Date createTime;
/** /** */
*/
private String userName; private String userName;
/** /** */
*/
private Integer queryState; private Integer queryState;
/** /** */
*/
private Long chatId; private Long chatId;
/** /** */
*/
private Integer score; private Integer score;
/** /** */
*/
private String feedback; private String feedback;
/** /** */
*/
private String queryText; private String queryText;
/** /** */
*/
private String queryResult; private String queryResult;
private String similarQueries; private String similarQueries;
private String parseTimeCost; private String parseTimeCost;
} }

View File

@@ -1,9 +1,9 @@
package com.tencent.supersonic.chat.server.persistence.dataobject; package com.tencent.supersonic.chat.server.persistence.dataobject;
import java.util.Date;
import lombok.Data; import lombok.Data;
import java.util.Date;
@Data @Data
public class DictConfDO { public class DictConfDO {
@@ -17,5 +17,4 @@ public class DictConfDO {
private String updatedBy; private String updatedBy;
private Date createdAt; private Date createdAt;
private Date updatedAt; private Date updatedAt;
} }

View File

@@ -1,11 +1,11 @@
package com.tencent.supersonic.chat.server.persistence.dataobject; package com.tencent.supersonic.chat.server.persistence.dataobject;
import java.util.Date;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import java.util.Date;
@Data @Data
@ToString @ToString
public class DictTaskDO { public class DictTaskDO {

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.server.persistence.dataobject; package com.tencent.supersonic.chat.server.persistence.dataobject;
import com.tencent.supersonic.headless.core.config.DefaultMetric; import com.tencent.supersonic.headless.core.config.DefaultMetric;
import com.tencent.supersonic.headless.core.config.Dim4Dict; import com.tencent.supersonic.headless.core.config.Dim4Dict;
import lombok.Data; import lombok.Data;
@@ -9,7 +8,6 @@ import lombok.ToString;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Data @Data
@ToString @ToString
public class DimValueDO { public class DimValueDO {

View File

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
import java.util.Date; import java.util.Date;
@Data @Data
@@ -36,5 +37,4 @@ public class PluginDO {
private String config; private String config;
private String comment; private String comment;
} }

View File

@@ -1,10 +1,10 @@
package com.tencent.supersonic.chat.server.persistence.dataobject; package com.tencent.supersonic.chat.server.persistence.dataobject;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.Date; import java.util.Date;
@@ -14,40 +14,25 @@ import java.util.Date;
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public class StatisticsDO { public class StatisticsDO {
/** /** questionId */
* questionId
*/
private Long questionId; private Long questionId;
/** /** chatId */
* chatId
*/
private Long chatId; private Long chatId;
/** /** createTime */
* createTime
*/
private Date createTime; private Date createTime;
/** /** queryText */
* queryText
*/
private String queryText; private String queryText;
/** /** userName */
* userName
*/
private String userName; private String userName;
/** interface */
/**
* interface
*/
private String interfaceName; private String interfaceName;
/** /** cost */
* cost
*/
private Integer cost; private Integer cost;
private Integer type; private Integer type;

View File

@@ -5,6 +5,4 @@ import com.tencent.supersonic.chat.server.persistence.dataobject.AgentDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface AgentDOMapper extends BaseMapper<AgentDO> { public interface AgentDOMapper extends BaseMapper<AgentDO> {}
}

Some files were not shown because too many files have changed in this diff Show More