(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;
import javax.servlet.http.HttpServletRequest;
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.request.UserReq;
import java.util.List;
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 {
List<String> getUserNames();

View File

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

View File

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

View File

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

View File

@@ -20,7 +20,8 @@ public class User {
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);
}
@@ -44,5 +45,4 @@ public class User {
public boolean isSuperAdmin() {
return isAdmin != null && isAdmin == 1;
}
}

View File

@@ -9,14 +9,24 @@ public class UserWithPassword extends User {
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);
this.password = password;
}
public static UserWithPassword get(Long id, String name, String displayName,
String email, String password, Integer isAdmin) {
public static UserWithPassword get(
Long id,
String name,
String displayName,
String email,
String password,
Integer isAdmin) {
return new UserWithPassword(id, name, displayName, email, password, isAdmin);
}
}

View File

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

View File

@@ -1,17 +1,19 @@
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.User;
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.Set;
public interface UserService {
User getCurrentUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);
User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);
List<String> getUserNames();

View File

@@ -1,10 +1,10 @@
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.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.pojo.User;
public interface UserStrategy {
boolean accept(boolean isEnableAuthentication);
@@ -12,5 +12,4 @@ public interface UserStrategy {
User findUser(HttpServletRequest request, HttpServletResponse response);
User findUser(String token, String appKey);
}

View File

@@ -1,12 +1,13 @@
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.service.UserStrategy;
import com.tencent.supersonic.common.config.SystemConfig;
import com.tencent.supersonic.common.service.SystemConfigService;
import com.tencent.supersonic.common.util.ContextUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.CollectionUtils;
public final class UserHolder {
@@ -36,5 +37,4 @@ public final class UserHolder {
}
return user;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,8 +1,9 @@
package com.tencent.supersonic.auth.api.authorization.request;
import java.util.List;
import lombok.Data;
import java.util.List;
@Data
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.request.QueryAuthResReq;
import com.tencent.supersonic.auth.api.authorization.response.AuthorizedResourceResp;
import java.util.List;
public interface AuthService {

View File

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

View File

@@ -1,5 +1,7 @@
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.constant.UserConstants;
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.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
@@ -21,14 +22,12 @@ import java.util.List;
@Slf4j
public abstract class AuthenticationInterceptor implements HandlerInterceptor {
protected AuthenticationConfig authenticationConfig;
protected UserServiceImpl userServiceImpl;
protected UserTokenUtils userTokenUtils;
protected S2ThreadContext s2ThreadContext;
protected boolean isExcludedUri(String uri) {
@@ -69,7 +68,8 @@ public abstract class AuthenticationInterceptor implements HandlerInterceptor {
try {
if (request instanceof StandardMultipartHttpServletRequest) {
RequestFacade servletRequest =
(RequestFacade) ((StandardMultipartHttpServletRequest) request).getRequest();
(RequestFacade)
((StandardMultipartHttpServletRequest) request).getRequest();
Class<? extends HttpServletRequest> servletRequestClazz = servletRequest.getClass();
Field request1 = servletRequestClazz.getDeclaredField("request");
request1.setAccessible(true);

View File

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

View File

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

View File

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

View File

@@ -4,101 +4,64 @@ import java.util.ArrayList;
import java.util.List;
public class UserDOExample {
/**
* s2_user
*/
/** s2_user */
protected String orderByClause;
/**
* s2_user
*/
/** s2_user */
protected boolean distinct;
/**
* s2_user
*/
/** s2_user */
protected List<Criteria> oredCriteria;
/**
* s2_user
*/
/** s2_user */
protected Integer limitStart;
/**
* s2_user
*/
/** s2_user */
protected Integer limitEnd;
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public UserDOExample() {
oredCriteria = new ArrayList<Criteria>();
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public String getOrderByClause() {
return orderByClause;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public boolean isDistinct() {
return distinct;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
@@ -107,60 +70,40 @@ public class UserDOExample {
return criteria;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public void setLimitStart(Integer limitStart) {
this.limitStart=limitStart;
this.limitStart = limitStart;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public Integer getLimitStart() {
return limitStart;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public void setLimitEnd(Integer limitEnd) {
this.limitEnd=limitEnd;
this.limitEnd = limitEnd;
}
/**
*
* @mbg.generated
*/
/** @mbg.generated */
public Integer getLimitEnd() {
return limitEnd;
}
/**
* s2_user null
*/
/** s2_user null */
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
@@ -195,7 +138,8 @@ public class UserDOExample {
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) {
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 {
protected Criteria() {
@@ -613,9 +555,7 @@ public class UserDOExample {
}
}
/**
* s2_user null
*/
/** s2_user null */
public static class Criterion {
private String condition;
@@ -688,7 +628,8 @@ public class UserDOExample {
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();
this.condition = condition;
this.value = value;
@@ -701,4 +642,4 @@ public class UserDOExample {
this(condition, value, secondValue, null);
}
}
}
}

View File

@@ -1,22 +1,17 @@
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.UserDOExample;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserDOMapper {
/**
* @mbg.generated
*/
/** @mbg.generated */
int insert(UserDO record);
/**
* @mbg.generated
*/
/** @mbg.generated */
List<UserDO> selectByExample(UserDOExample example);
}

View File

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

View File

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

View File

@@ -1,5 +1,7 @@
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.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.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;
@@ -30,7 +30,8 @@ public class UserController {
}
@GetMapping("/getCurrentUser")
public User getCurrentUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
public User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
return userService.getCurrentUser(httpServletRequest, httpServletResponse);
}
@@ -68,5 +69,4 @@ public class UserController {
public String login(@RequestBody UserReq userCmd, HttpServletRequest request) {
return userService.login(userCmd, request);
}
}

View File

@@ -1,5 +1,8 @@
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.User;
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 org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;
@@ -25,7 +27,8 @@ public class UserServiceImpl implements UserService {
}
@Override
public User getCurrentUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
public User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
User user = UserHolder.findUser(httpServletRequest, httpServletResponse);
if (user != null) {
SystemConfig systemConfig = sysParameterService.getSystemConfig();
@@ -76,5 +79,4 @@ public class UserServiceImpl implements UserService {
public String login(UserReq userReq, String appKey) {
return ComponentFactory.getUserAdaptor().login(userReq, appKey);
}
}

View File

@@ -1,10 +1,10 @@
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.service.UserStrategy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;
@Service
@@ -24,5 +24,4 @@ public class FakeUserStrategy implements UserStrategy {
public User findUser(String token, String appKey) {
return User.getFakeUser();
}
}

View File

@@ -1,20 +1,18 @@
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.service.UserStrategy;
import com.tencent.supersonic.auth.authentication.utils.UserTokenUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;
@Service
public class HttpHeaderUserStrategy implements UserStrategy {
private final UserTokenUtils userTokenUtils;
public HttpHeaderUserStrategy(UserTokenUtils userTokenUtils) {
this.userTokenUtils = userTokenUtils;
}

View File

@@ -1,26 +1,25 @@
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.service.UserStrategy;
import com.tencent.supersonic.auth.api.authentication.utils.UserHolder;
import java.util.List;
import javax.annotation.PostConstruct;
import lombok.Data;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Configuration
@Data
public class UserStrategyFactory {
private List<UserStrategy> userStrategyList;
private AuthenticationConfig authenticationConfig;
public UserStrategyFactory(AuthenticationConfig authenticationConfig, List<UserStrategy> userStrategyList) {
public UserStrategyFactory(
AuthenticationConfig authenticationConfig, List<UserStrategy> userStrategyList) {
this.authenticationConfig = authenticationConfig;
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 org.springframework.core.io.support.SpringFactoriesLoader;
import java.util.Objects;
public class ComponentFactory {
@@ -16,8 +17,8 @@ public class ComponentFactory {
}
private static <T> T init(Class<T> factoryType) {
return SpringFactoriesLoader.loadFactories(factoryType,
Thread.currentThread().getContextClassLoader()).get(0);
return SpringFactoriesLoader.loadFactories(
factoryType, Thread.currentThread().getContextClassLoader())
.get(0);
}
}

View File

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

View File

@@ -1,14 +1,14 @@
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.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.response.AuthorizedResourceResp;
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 org.springframework.web.bind.annotation.GetMapping;
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.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/auth")
@Slf4j
@@ -29,14 +31,13 @@ public class AuthController {
}
@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) {
return authService.queryAuthGroups(modelId, groupId);
}
/**
* 新建权限组
*/
/** 新建权限组 */
@PostMapping("/createGroup")
public void newAuthGroup(@RequestBody AuthGroup group) {
group.setGroupId(null);
@@ -68,9 +69,10 @@ public class AuthController {
* @return
*/
@PostMapping("/queryAuthorizedRes")
public AuthorizedResourceResp queryAuthorizedResources(@RequestBody QueryAuthResReq req,
HttpServletRequest request,
HttpServletResponse response) {
public AuthorizedResourceResp queryAuthorizedResources(
@RequestBody QueryAuthResReq req,
HttpServletRequest request,
HttpServletResponse response) {
User user = UserHolder.findUser(request, response);
return authService.queryAuthorizedResources(req, user);
}

View File

@@ -30,23 +30,27 @@ public class AuthServiceImpl implements AuthService {
private UserService userService;
public AuthServiceImpl(JdbcTemplate jdbcTemplate,
UserService userService) {
public AuthServiceImpl(JdbcTemplate jdbcTemplate, UserService userService) {
this.jdbcTemplate = jdbcTemplate;
this.userService = userService;
}
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();
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
public List<AuthGroup> queryAuthGroups(String modelId, Integer groupId) {
return load().stream()
.filter(group -> (Objects.isNull(groupId) || groupId.equals(group.getGroupId()))
&& modelId.equals(group.getModelId().toString()))
.filter(
group ->
(Objects.isNull(groupId) || groupId.equals(group.getGroupId()))
&& modelId.equals(group.getModelId().toString()))
.collect(Collectors.toList());
}
@@ -61,10 +65,14 @@ public class AuthServiceImpl implements AuthService {
nextGroupId = obj + 1;
}
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));
} 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());
}
}
@@ -80,10 +88,11 @@ public class AuthServiceImpl implements AuthService {
return new AuthorizedResourceResp();
}
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();
Map<Long, List<AuthGroup>> authGroupsByModelId = groups.stream()
.collect(Collectors.groupingBy(AuthGroup::getModelId));
Map<Long, List<AuthGroup>> authGroupsByModelId =
groups.stream().collect(Collectors.groupingBy(AuthGroup::getModelId));
for (Long modelId : req.getModelIds()) {
if (authGroupsByModelId.containsKey(modelId)) {
List<AuthGroup> authGroups = authGroupsByModelId.get(modelId);
@@ -110,26 +119,31 @@ public class AuthServiceImpl implements AuthService {
return resource;
}
private List<AuthGroup> getAuthGroups(List<Long> modelIds, String userName, List<String> departmentIds) {
List<AuthGroup> groups = load().stream()
.filter(group -> {
if (!modelIds.contains(group.getModelId())) {
return false;
}
if (!CollectionUtils.isEmpty(group.getAuthorizedUsers())
&& group.getAuthorizedUsers().contains(userName)) {
return true;
}
for (String departmentId : departmentIds) {
if (!CollectionUtils.isEmpty(group.getAuthorizedDepartmentIds())
&& group.getAuthorizedDepartmentIds().contains(departmentId)) {
return true;
}
}
return false;
}).collect(Collectors.toList());
private List<AuthGroup> getAuthGroups(
List<Long> modelIds, String userName, List<String> departmentIds) {
List<AuthGroup> groups =
load().stream()
.filter(
group -> {
if (!modelIds.contains(group.getModelId())) {
return false;
}
if (!CollectionUtils.isEmpty(group.getAuthorizedUsers())
&& group.getAuthorizedUsers().contains(userName)) {
return true;
}
for (String departmentId : departmentIds) {
if (!CollectionUtils.isEmpty(
group.getAuthorizedDepartmentIds())
&& group.getAuthorizedDepartmentIds()
.contains(departmentId)) {
return true;
}
}
return false;
})
.collect(Collectors.toList());
log.info("user:{} department:{} authGroups:{}", userName, departmentIds, groups);
return groups;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -2,9 +2,8 @@ package com.tencent.supersonic.chat.api.pojo.request;
import lombok.Data;
@Data
public class ChatConfigEditReqReq extends ChatConfigBaseReq {
private Long id;
}
}

View File

@@ -4,7 +4,6 @@ import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@Data
public class ChatConfigFilter {
@@ -12,4 +11,4 @@ public class ChatConfigFilter {
private Long id;
private Long modelId;
private StatusEnum status = StatusEnum.ONLINE;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,13 +1,12 @@
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.MemoryStatus;
import com.tencent.supersonic.common.pojo.RecordInfo;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
public class ChatMemoryUpdateReq extends RecordInfo {
@@ -23,5 +22,4 @@ public class ChatMemoryUpdateReq extends RecordInfo {
private MemoryReviewResult humanReviewRet;
private String humanReviewCmt;
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,13 +5,11 @@ import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* advanced knowledge config
*/
/** advanced knowledge config */
@Data
public class KnowledgeAdvancedConfig {
private List<String> blackList = new ArrayList<>();
private List<String> whiteList = new ArrayList<>();
private List<String> ruleList = new ArrayList<>();
}
}

View File

@@ -1,35 +1,23 @@
package com.tencent.supersonic.chat.api.pojo.request;
import com.tencent.supersonic.common.pojo.enums.TypeEnums;
import javax.validation.constraints.NotNull;
import com.tencent.supersonic.common.pojo.enums.TypeEnums;
import lombok.Data;
/**
* information about dictionary about the model
*/
/** information about dictionary about the model */
@Data
public class KnowledgeInfoReq {
/**
* metricId、DimensionId、modelId
*/
/** metricId、DimensionId、modelId */
private Long itemId;
private String bizName;
/**
* type: IntentionTypeEnum
* temporarily only supports dimension-related information
*/
@NotNull
private TypeEnums type = TypeEnums.DIMENSION;
/** type: IntentionTypeEnum temporarily only supports dimension-related information */
@NotNull private TypeEnums type = TypeEnums.DIMENSION;
private Boolean searchEnable = false;
/**
* advanced knowledge config for single item
*/
/** advanced knowledge config for single item */
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 lombok.Data;
@Data
public class PageMemoryReq extends PageBaseReq {
private ChatMemoryFilter chatMemoryFilter = new ChatMemoryFilter();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,18 +9,13 @@ import java.util.List;
@Data
public class ChatAggRichConfigResp {
/**
* invisible dimensions/metrics
*/
/** invisible dimensions/metrics */
private ItemVisibilityInfo visibility;
/**
* information about dictionary about the model
*/
/** information about dictionary about the model */
private List<KnowledgeInfoReq> knowledgeInfos;
private KnowledgeAdvancedConfig globalKnowledgeConfig;
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.RecommendedQuestionReq;
import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import lombok.Data;
import java.util.Date;
import java.util.List;
import lombok.Data;
@Data
public class ChatConfigResp {
@@ -25,13 +24,11 @@ public class ChatConfigResp {
private String llmExamples;
/**
* available status
*/
/** available status */
private StatusEnum statusEnum;
private String createdBy;
private String updatedBy;
private Date createdAt;
private Date updatedAt;
}
}

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.common.pojo.enums.StatusEnum;
import lombok.Data;
import java.util.Date;
import java.util.List;
import lombok.Data;
@Data
public class ChatConfigRichResp {
@@ -23,9 +23,7 @@ public class ChatConfigRichResp {
private List<RecommendedQuestionReq> recommendedQuestions;
/**
* available status
*/
/** available status */
private StatusEnum statusEnum;
private String createdBy;

View File

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

View File

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

View File

@@ -27,4 +27,4 @@ public class DictLatestTaskResp {
private Date createdAt;
private Long elapsedMs;
}
}

View File

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

View File

@@ -1,8 +1,8 @@
package com.tencent.supersonic.chat.api.pojo.response;
import java.util.List;
import lombok.Data;
import java.util.List;
@Data
public class ItemVisibilityInfo {
@@ -11,4 +11,4 @@ public class ItemVisibilityInfo {
private List<Long> blackMetricIdList;
private List<Long> whiteDimIdList;
private List<Long> whiteMetricIdList;
}
}

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.response.ParseTimeCostResp;
import lombok.Data;
import java.util.Date;
import java.util.List;
@Data
public class QueryResp {
@@ -20,5 +20,4 @@ public class QueryResp {
private List<SemanticParseInfo> parseInfos;
private List<SimilarQueryRecallResp> similarQueries;
private ParseTimeCostResp parseTimeCost = new ParseTimeCostResp();
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,5 @@
package com.tencent.supersonic.chat.server.agent;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
@@ -9,7 +8,6 @@ import java.util.List;
@Data
public class RuleParserTool extends NL2SQLTool {
private List<String> queryModes;
private List<String> queryTypes;
@@ -17,5 +15,4 @@ public class RuleParserTool extends NL2SQLTool {
public boolean isContainsAllModel() {
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.ChatDetailConfigReq;
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.enums.StatusEnum;
import lombok.Data;
import lombok.ToString;
@@ -14,33 +14,22 @@ import java.util.List;
@ToString
public class ChatConfig {
/**
* database auto-increment primary key
*/
/** database auto-increment primary key */
private Long id;
private Long modelId;
/**
* the chatDetailConfig about the model
*/
/** the chatDetailConfig about the model */
private ChatDetailConfigReq chatDetailConfig;
/**
* the chatAggConfig about the model
*/
/** the chatAggConfig about the model */
private ChatAggConfigReq chatAggConfig;
private List<RecommendedQuestionReq> recommendedQuestions;
/**
* available status
*/
/** available status */
private StatusEnum status;
/**
* about createdBy, createdAt, updatedBy, updatedAt
*/
/** about createdBy, createdAt, updatedBy, updatedAt */
private RecordInfo recordInfo;
}

View File

@@ -8,4 +8,4 @@ public class ChatConfigFilterInternal {
private Long id;
private Long modelId;
private Integer status;
}
}

View File

@@ -1,10 +1,9 @@
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.server.pojo.ExecuteContext;
public interface ChatQueryExecutor {
QueryResult execute(ExecuteContext executeContext);
}

View File

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

View File

@@ -1,10 +1,10 @@
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.build.PluginSemanticQuery;
import com.tencent.supersonic.chat.server.pojo.ExecuteContext;
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
import com.tencent.supersonic.chat.api.pojo.response.QueryResult;
public class PluginExecutor implements ChatQueryExecutor {
@@ -18,5 +18,4 @@ public class PluginExecutor implements ChatQueryExecutor {
query.setParseInfo(parseInfo);
return query.build();
}
}

View File

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

View File

@@ -27,27 +27,26 @@ public class MemoryReviewTask {
private static final Logger keyPipelineLog = LoggerFactory.getLogger("keyPipeline");
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,"
+ "please take a review and give your opinion.\n"
+ "#Rules: "
+ "1.ALWAYS follow the output format: `opinion=(POSITIVE|NEGATIVE),comment=(your comment)`."
+ "2.ALWAYS recognize `数据日期` as the date field."
+ "3.IGNORE `数据日期` if not expressed in the `Question`."
+ "#Question: %s\n"
+ "#Schema: %s\n"
+ "#SideInfo: %s\n"
+ "#SQL: %s\n"
+ "#Response: ";
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,"
+ "please take a review and give your opinion.\n"
+ "#Rules: "
+ "1.ALWAYS follow the output format: `opinion=(POSITIVE|NEGATIVE),comment=(your comment)`."
+ "2.ALWAYS recognize `数据日期` as the date field."
+ "3.IGNORE `数据日期` if not expressed in the `Question`."
+ "#Question: %s\n"
+ "#Schema: %s\n"
+ "#SideInfo: %s\n"
+ "#SQL: %s\n"
+ "#Response: ";
private static final Pattern OUTPUT_PATTERN = Pattern.compile("opinion=(.*),.*comment=(.*)");
@Autowired
private MemoryService memoryService;
@Autowired private MemoryService memoryService;
@Autowired
private AgentService agentService;
@Autowired private AgentService agentService;
@Scheduled(fixedDelay = 60 * 1000)
public void review() {
@@ -68,7 +67,8 @@ public class MemoryReviewTask {
Prompt prompt = PromptTemplate.from(promptStr).apply(Collections.EMPTY_MAP);
keyPipelineLog.info("MemoryReviewTask reqPrompt:\n{}", promptStr);
ChatLanguageModel chatLanguageModel = ModelProvider.getChatModel(chatAgent.getModelConfig());
ChatLanguageModel chatLanguageModel =
ModelProvider.getChatModel(chatAgent.getModelConfig());
if (Objects.nonNull(chatLanguageModel)) {
String response = chatLanguageModel.generate(prompt.toUserMessage()).content().text();
keyPipelineLog.info("MemoryReviewTask modelResp:\n{}", response);
@@ -79,7 +79,8 @@ public class MemoryReviewTask {
}
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) {

View File

@@ -6,5 +6,4 @@ import com.tencent.supersonic.headless.api.pojo.response.ParseResp;
public interface ChatQueryParser {
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.headless.api.pojo.response.ParseResp;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
@Slf4j
public class NL2PluginParser implements ChatQueryParser {
private final List<PluginRecognizer> pluginRecognizers = ComponentFactory.getPluginRecognizers();
private final List<PluginRecognizer> pluginRecognizers =
ComponentFactory.getPluginRecognizers();
@Override
public void parse(ParseContext parseContext, ParseResp parseResp) {
@@ -19,11 +21,13 @@ public class NL2PluginParser implements ChatQueryParser {
return;
}
pluginRecognizers.forEach(pluginRecognizer -> {
pluginRecognizer.recognize(parseContext, parseResp);
log.info("{} recallResult:{}", pluginRecognizer.getClass().getSimpleName(),
JsonUtil.toString(parseResp));
});
pluginRecognizers.forEach(
pluginRecognizer -> {
pluginRecognizer.recognize(parseContext, 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.server.agent.MultiTurnConfig;
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.service.ChatContextService;
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.response.MapResp;
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.server.facade.service.ChatLayerService;
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 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,"
+ "along with their mapped schema elements(metric, dimension and value),"
+ "please try understanding the semantics and rewrite a question."
+ "#Rules: "
+ "1.ALWAYS keep relevant entities, metrics, dimensions, values and date ranges."
+ "2.ONLY respond with the rewritten question."
+ "#Current Question: {{current_question}}"
+ "#Current Mapped Schema: {{current_schema}}"
+ "#History Question: {{history_question}}"
+ "#History Mapped Schema: {{history_schema}}"
+ "#History SQL: {{history_sql}}"
+ "#Rewritten Question: ";
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,"
+ "along with their mapped schema elements(metric, dimension and value),"
+ "please try understanding the semantics and rewrite a question."
+ "#Rules: "
+ "1.ALWAYS keep relevant entities, metrics, dimensions, values and date ranges."
+ "2.ONLY respond with the rewritten question."
+ "#Current Question: {{current_question}}"
+ "#Current Mapped Schema: {{current_schema}}"
+ "#History Question: {{history_question}}"
+ "#History Mapped Schema: {{history_schema}}"
+ "#History SQL: {{history_sql}}"
+ "#Rewritten Question: ";
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, "
+ "please respond shortly to teach user how to ask the right question, "
+ "by using `Examples` as references."
+ "#Rules: ALWAYS respond with the same language as the `Input`.\n"
+ "#Input: {{user_question}}\n"
+ "#Output: {{system_message}}\n"
+ "#Examples: {{examples}}\n"
+ "#Response: ";
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, "
+ "please respond shortly to teach user how to ask the right question, "
+ "by using `Examples` as references."
+ "#Rules: ALWAYS respond with the same language as the `Input`.\n"
+ "#Input: {{user_question}}\n"
+ "#Output: {{system_message}}\n"
+ "#Examples: {{examples}}\n"
+ "#Response: ";
@Override
public void parse(ParseContext parseContext, ParseResp parseResp) {
@@ -84,8 +86,8 @@ public class NL2SQLParser implements ChatQueryParser {
ChatContextService chatContextService = ContextUtils.getBean(ChatContextService.class);
ChatContext chatCtx = chatContextService.getOrCreateContext(parseContext.getChatId());
ChatLanguageModel chatLanguageModel = ModelProvider.getChatModel(
parseContext.getAgent().getModelConfig());
ChatLanguageModel chatLanguageModel =
ModelProvider.getChatModel(parseContext.getAgent().getModelConfig());
processMultiTurn(chatLanguageModel, parseContext);
QueryNLReq queryNLReq = QueryReqConverter.buildText2SqlQueryReq(parseContext, chatCtx);
@@ -96,11 +98,13 @@ public class NL2SQLParser implements ChatQueryParser {
if (ParseResp.ParseState.COMPLETED.equals(text2SqlParseResp.getState())) {
parseResp.getSelectedParses().addAll(text2SqlParseResp.getSelectedParses());
} else {
parseResp.setErrorMsg(rewriteErrorMessage(chatLanguageModel,
parseContext.getQueryText(),
text2SqlParseResp.getErrorMsg(),
queryNLReq.getDynamicExemplars(),
parseContext.getAgent().getExamples()));
parseResp.setErrorMsg(
rewriteErrorMessage(
chatLanguageModel,
parseContext.getQueryText(),
text2SqlParseResp.getErrorMsg(),
queryNLReq.getDynamicExemplars(),
parseContext.getAgent().getExamples()));
}
parseResp.setState(text2SqlParseResp.getState());
parseResp.getParseTimeCost().setSqlTime(text2SqlParseResp.getParseTimeCost().getSqlTime());
@@ -134,24 +138,35 @@ public class NL2SQLParser implements ChatQueryParser {
StringBuilder textBuilder = new StringBuilder();
textBuilder.append("**数据集:** ").append(parseInfo.getDataSet().getName()).append(" ");
Optional<SchemaElement> metric = parseInfo.getMetrics().stream().findFirst();
metric.ifPresent(schemaElement ->
textBuilder.append("**指标:** ").append(schemaElement.getName()).append(" "));
List<String> dimensionNames = parseInfo.getDimensions().stream()
.map(SchemaElement::getName).filter(Objects::nonNull).collect(Collectors.toList());
metric.ifPresent(
schemaElement ->
textBuilder.append("**指标:** ").append(schemaElement.getName()).append(" "));
List<String> dimensionNames =
parseInfo.getDimensions().stream()
.map(SchemaElement::getName)
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(dimensionNames)) {
textBuilder.append("**维度:** ").append(String.join(",", dimensionNames));
}
textBuilder.append("\n\n**筛选条件:** \n");
if (parseInfo.getDateInfo() != null) {
textBuilder.append("**数据时间:** ").append(parseInfo.getDateInfo().getStartDate()).append("~")
.append(parseInfo.getDateInfo().getEndDate()).append(" ");
textBuilder
.append("**数据时间:** ")
.append(parseInfo.getDateInfo().getStartDate())
.append("~")
.append(parseInfo.getDateInfo().getEndDate())
.append(" ");
}
if (!CollectionUtils.isEmpty(parseInfo.getDimensionFilters())
|| CollectionUtils.isEmpty(parseInfo.getMetricFilters())) {
Set<QueryFilter> queryFilters = parseInfo.getDimensionFilters();
queryFilters.addAll(parseInfo.getMetricFilters());
for (QueryFilter queryFilter : queryFilters) {
textBuilder.append("**").append(queryFilter.getName()).append("**")
textBuilder
.append("**")
.append(queryFilter.getName())
.append("**")
.append(" ")
.append(queryFilter.getOperator().getValue())
.append(" ")
@@ -165,10 +180,13 @@ public class NL2SQLParser implements ChatQueryParser {
private void processMultiTurn(ChatLanguageModel chatLanguageModel, ParseContext parseContext) {
ParserConfig parserConfig = ContextUtils.getBean(ParserConfig.class);
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
? agentMultiTurnConfig.isEnableMultiTurn() : globalMultiTurnConfig;
Boolean multiTurnConfig =
agentMultiTurnConfig != null
? agentMultiTurnConfig.isEnableMultiTurn()
: globalMultiTurnConfig;
if (!Boolean.TRUE.equals(multiTurnConfig)) {
return;
}
@@ -186,7 +204,8 @@ public class NL2SQLParser implements ChatQueryParser {
SemanticParseInfo lastParseInfo = lastQuery.getParseInfos().get(0);
Long dataId = lastParseInfo.getDataSetId();
String curtMapStr = generateSchemaPrompt(currentMapResult.getMapInfo().getMatchedElements(dataId));
String curtMapStr =
generateSchemaPrompt(currentMapResult.getMapInfo().getMatchedElements(dataId));
String histMapStr = generateSchemaPrompt(lastParseInfo.getElementMatches());
String histSQL = lastParseInfo.getSqlInfo().getCorrectedS2SQL();
@@ -207,22 +226,31 @@ public class NL2SQLParser implements ChatQueryParser {
QueryNLReq rewrittenQueryNLReq = QueryReqConverter.buildText2SqlQueryReq(parseContext);
MapResp rewrittenQueryMapResult = chatLayerService.performMapping(rewrittenQueryNLReq);
parseContext.setMapInfo(rewrittenQueryMapResult.getMapInfo());
log.info("Last Query: {} Current Query: {}, Rewritten Query: {}",
lastQuery.getQueryText(), currentMapResult.getQueryText(), rewrittenQuery);
log.info(
"Last Query: {} Current Query: {}, Rewritten Query: {}",
lastQuery.getQueryText(),
currentMapResult.getQueryText(),
rewrittenQuery);
}
private String rewriteErrorMessage(ChatLanguageModel chatLanguageModel, String userQuestion,
String errMsg, List<Text2SQLExemplar> similarExemplars,
List<String> agentExamples) {
private String rewriteErrorMessage(
ChatLanguageModel chatLanguageModel,
String userQuestion,
String errMsg,
List<Text2SQLExemplar> similarExemplars,
List<String> agentExamples) {
Map<String, Object> variables = new HashMap<>();
variables.put("user_question", userQuestion);
variables.put("system_message", errMsg);
StringBuilder exampleStr = new StringBuilder();
similarExemplars.forEach(e ->
exampleStr.append(String.format("<Question:{%s},Schema:{%s}> ", e.getQuestion(), e.getDbSchema())));
agentExamples.forEach(e ->
exampleStr.append(String.format("<Question:{%s}> ", e)));
similarExemplars.forEach(
e ->
exampleStr.append(
String.format(
"<Question:{%s},Schema:{%s}> ",
e.getQuestion(), e.getDbSchema())));
agentExamples.forEach(e -> exampleStr.append(String.format("<Question:{%s}> ", e)));
variables.put("examples", exampleStr);
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) {
ChatManageService chatManageService = ContextUtils.getBean(ChatManageService.class);
List<QueryResp> contextualParseInfoList = chatManageService.getChatQueries(chatId)
.stream()
.filter(q -> Objects.nonNull(q.getQueryResult())
&& q.getQueryResult().getQueryState() == QueryState.SUCCESS)
.collect(Collectors.toList());
List<QueryResp> contextualParseInfoList =
chatManageService.getChatQueries(chatId).stream()
.filter(
q ->
Objects.nonNull(q.getQueryResult())
&& q.getQueryResult().getQueryState()
== QueryState.SUCCESS)
.collect(Collectors.toList());
List<QueryResp> contextualList = contextualParseInfoList.subList(0,
Math.min(multiNum, contextualParseInfoList.size()));
List<QueryResp> contextualList =
contextualParseInfoList.subList(
0, Math.min(multiNum, contextualParseInfoList.size()));
Collections.reverse(contextualList);
return contextualList;
}
@@ -278,9 +310,8 @@ public class NL2SQLParser implements ChatQueryParser {
ExemplarServiceImpl exemplarManager = ContextUtils.getBean(ExemplarServiceImpl.class);
EmbeddingConfig embeddingConfig = ContextUtils.getBean(EmbeddingConfig.class);
String memoryCollectionName = embeddingConfig.getMemoryCollectionName(agentId);
List<Text2SQLExemplar> exemplars = exemplarManager.recallExemplars(memoryCollectionName,
queryNLReq.getQueryText(), 5);
List<Text2SQLExemplar> exemplars =
exemplarManager.recallExemplars(memoryCollectionName, queryNLReq.getQueryText(), 5);
queryNLReq.getDynamicExemplars().addAll(exemplars);
}
}

View File

@@ -10,8 +10,11 @@ import org.springframework.stereotype.Service;
public class ParserConfig extends ParameterConfig {
public static final Parameter PARSER_MULTI_TURN_ENABLE =
new Parameter("s2.parser.multi-turn.enable", "false",
"是否开启多轮对话", "开启多轮对话将消耗更多token",
"bool", "Parser相关配置");
new Parameter(
"s2.parser.multi-turn.enable",
"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.response.ParseResp;
public class PlainTextParser implements ChatQueryParser {
@Override
@@ -18,5 +17,4 @@ public class PlainTextParser implements ChatQueryParser {
parseResp.getSelectedParses().add(parseInfo);
parseResp.setState(ParseResp.ParseState.COMPLETED);
}
}

View File

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

View File

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

View File

@@ -61,5 +61,4 @@ public class ChatMemoryDO {
@TableField("updated_at")
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.TableName;
import lombok.Data;
import java.util.Date;
@Data
@TableName("s2_chat_query")
public class ChatQueryDO {
/**
*/
/** */
@TableId(type = IdType.AUTO)
private Long questionId;
/**
*/
/** */
private Integer agentId;
/**
*/
/** */
private Date createTime;
/**
*/
/** */
private String userName;
/**
*/
/** */
private Integer queryState;
/**
*/
/** */
private Long chatId;
/**
*/
/** */
private Integer score;
/**
*/
/** */
private String feedback;
/**
*/
/** */
private String queryText;
/**
*/
/** */
private String queryResult;
private String similarQueries;
private String parseTimeCost;
}

View File

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

View File

@@ -1,11 +1,11 @@
package com.tencent.supersonic.chat.server.persistence.dataobject;
import java.util.Date;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.codec.digest.DigestUtils;
import java.util.Date;
@Data
@ToString
public class DictTaskDO {
@@ -35,4 +35,4 @@ public class DictTaskDO {
public String getCommandMd5() {
return DigestUtils.md5Hex(command);
}
}
}

View File

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

View File

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

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