Skip to content

Commit

Permalink
Merge branch 'refs/heads/feat/0.3.2.1' into feat/0.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
zgqgit committed Jul 3, 2024
2 parents e8707e1 + 5ea6378 commit 157ae72
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 103 deletions.
9 changes: 9 additions & 0 deletions src/backend/bisheng/api/services/role_group_service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json
from datetime import datetime
from typing import List, Any, Dict
from uuid import UUID

from fastapi.encoders import jsonable_encoder
from fastapi import Request, HTTPException

from bisheng.cache.redis import redis_client
from bisheng.api.services.assistant import AssistantService
from bisheng.api.services.audit_log import AuditLogService
from bisheng.api.services.user_service import UserPayload
Expand Down Expand Up @@ -125,6 +127,13 @@ def delete_group_hook(self, request: Request, login_user: UserPayload, group_inf
GroupResourceDao.delete_group_resource_by_group_id(group_info.id)
# 删除用户组下的角色列表
RoleDao.delete_role_by_group_id(group_info.id)
# 删除用户组的管理员
UserGroupDao.delete_group_all_admin(group_info.id)
# 将删除事件发到redis队列中
delete_message = json.dumps({"id": group_info.id})
redis_client.rpush('delete_group', delete_message)
redis_client.expire_key('delete_group', 86400)
redis_client.publish('delete_group', delete_message)

def get_group_user_list(self, group_id: int, page_size: int, page_num: int) -> List[User]:
"""获取全量的group列表"""
Expand Down
25 changes: 21 additions & 4 deletions src/backend/bisheng/api/v1/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,18 @@ async def process_knowledge(*,
session.add(knowledge)
session.commit()

upload_knowledge_file_hook(request, login_user, knowledge_id, files)
return resp_200(result)


def upload_knowledge_file_hook(request: Request, login_user: UserPayload, knowledge_id: int,
file_list: List[KnowledgeFile]):
logger.info(f'act=upload_knowledge_file_hook user={login_user.user_name} knowledge_id={knowledge_id}')
# 记录审计日志
file_name = ""
for one in files:
for one in file_list:
file_name += "\n\n" + one.file_name
AuditLogService.upload_knowledge_file(login_user, get_request_ip(request), knowledge_id, file_name)
return resp_200(result)


@router.post('/create', response_model=UnifiedResponseModel[KnowledgeRead], status_code=201)
Expand Down Expand Up @@ -218,7 +224,7 @@ def create_knowledge_hook(request: Request, knowledge: Knowledge, login_user: Us
# 查询下用户所在的用户组
user_group = UserGroupDao.get_user_group(login_user.user_id)
if user_group:
# 批量将助手资源插入到关联表里
# 批量将知识库资源插入到关联表里
batch_resource = []
for one in user_group:
batch_resource.append(GroupResource(
Expand Down Expand Up @@ -436,6 +442,17 @@ def delete_knowledge_file(*,
KnowledgeFileDao.delete_batch([file_id])

# 删除知识库文件的审计日志
AuditLogService.delete_knowledge_file(login_user, get_request_ip(request), knowledge.id, knowledge_file.file_name)
delete_knowledge_file_hook(request, login_user, knowledge.id, [knowledge_file])

return resp_200(message='删除成功')


def delete_knowledge_file_hook(request: Request, login_user: UserPayload, knowledge_id: int,
file_list: List[KnowledgeFile]):
logger.info(f'act=delete_knowledge_file_hook user={login_user.user_name} knowledge_id={knowledge_id}')
# 记录审计日志
# 记录审计日志
file_name = ""
for one in file_list:
file_name += "\n\n" + one.file_name
AuditLogService.delete_knowledge_file(login_user, get_request_ip(request), knowledge_id, file_name)
66 changes: 0 additions & 66 deletions src/backend/bisheng/api/v1/usergroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,69 +201,3 @@ async def get_group_roles(*,
"data": role_list,
"total": total
})


@router.get("/assistant", response_model=UnifiedResponseModel[List[AssistantSimpleInfo]])
async def get_group_assistant(*,
group_id: int = Query(..., description="用户组ID"),
keyword: str = Query(None, description="搜索关键字"),
page: int = 1,
limit: int = 10,
user: UserPayload = Depends(get_login_user)):
"""
获取用户组可见的助手列表
"""
# 判断是否是用户组的管理员
if not user.check_group_admin(group_id):
return UnAuthorizedError.return_resp()

# 查询用户组下的助手ID列表
resource_list = GroupResourceDao.get_group_resource(group_id, ResourceTypeEnum.ASSISTANT)
res = []
total = 0
if resource_list:
assistant_ids = [UUID(resource.third_id) for resource in resource_list] # 查询助手
data, total = AssistantDao.filter_assistant_by_id(assistant_ids, keyword, page, limit)
for one in data:
simple_one = AssistantService.return_simple_assistant_info(one)
res.append(simple_one)
return resp_200(data={
"data": res,
"total": total
})


@router.get("/knowledge", response_model=UnifiedResponseModel[GroupRead])
async def get_group_knowledge(*,
group_id: int = Query(..., description="用户组ID"),
keyword: str = Query(None, description="搜索关键字"),
page: int = 1,
limit: int = 10,
user: UserPayload = Depends(get_login_user)):
"""
获取用户组可见的知识库列表
"""

# 判断是否是用户组的管理员
if not user.check_group_admin(group_id):
return UnAuthorizedError.return_resp()

# 查询用户组下的知识库ID列表
resource_list = GroupResourceDao.get_group_resource(group_id, ResourceTypeEnum.KNOWLEDGE)
res = []
total = 0
if resource_list:
knowledge_ids = [int(resource.third_id) for resource in resource_list]
# 查询知识库
data, total = KnowledgeDao.filter_knowledge_by_ids(knowledge_ids, keyword, page, limit)
db_user_ids = {one.user_id for one in data}
user_list = UserDao.get_user_by_ids(list(db_user_ids))
user_map = {user.user_id: user.user_name for user in user_list}
for one in data:
one_dict = one.model_dump()
one_dict["user_name"] = user_map.get(one.user_id, one.user_id)
res.append(one_dict)
return resp_200(data={
"data": res,
"total": total
})
11 changes: 1 addition & 10 deletions src/backend/bisheng/api/v2/filelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
delete_es, delete_knowledge_by,
delete_knowledge_file_vectors, delete_vector,
text_knowledge)
from bisheng.api.services.user_service import UserPayload
from bisheng.api.v1.knowledge import delete_knowledge_hook, create_knowledge_hook
from bisheng.api.v1.schemas import ChunkInput, UnifiedResponseModel, resp_200, resp_500
from bisheng.cache.utils import save_download_file
from bisheng.database.base import session_getter
Expand All @@ -33,16 +31,12 @@


@router.post('/', response_model=KnowledgeRead, status_code=201)
def creat(knowledge: KnowledgeCreate):
def create(knowledge: KnowledgeCreate):
"""创建知识库."""
user_id = knowledge.user_id or settings.get_from_db('default_operator').get('user')
if not user_id:
raise HTTPException(status_code=500, detail='未配置default_operator中user配置')
user_payload = UserPayload(**{
"user_id": user_id
})
db_knowledge = create_knowledge(knowledge, user_id)
create_knowledge_hook(db_knowledge, user_payload)
return db_knowledge


Expand Down Expand Up @@ -129,9 +123,6 @@ def delete_knowledge_api(*, knowledge_id: int):
raise HTTPException(status_code=404, detail='knowledge not found')
try:
delete_knowledge_by(knowledge)
delete_knowledge_hook(knowledge, UserPayload(**{
"user_id": knowledge.user_id
}))
return {'message': 'knowledge deleted successfully'}
except Exception as e:
logger.exception(e)
Expand Down
14 changes: 14 additions & 0 deletions src/backend/bisheng/cache/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ def delete(self, key):
finally:
self.close()

def rpush(self, key, value):
try:
self.cluster_nodes(key)
return self.connection.rpush(key, value)
finally:
self.close()

def publish(self, key, value):
try:
self.cluster_nodes(key)
return self.connection.publish(key, value)
finally:
self.close()

def exists(self, key):
try:
self.cluster_nodes(key)
Expand Down
4 changes: 2 additions & 2 deletions src/backend/bisheng/database/models/audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class AuditLogBase(SQLModelSerializable):
event_type: Optional[str] = Field(index=True, description="操作行为")
object_type: Optional[str] = Field(index=True, description="操作对象类型")
object_id: Optional[int] = Field(index=True, description="操作对象ID")
object_name: Optional[str] = Field(description="操作对象名称")
note: Optional[str] = Field(sa_column=Column(Text(255)), description="操作备注")
object_name: Optional[str] = Field(sa_column=Column(Text), description="操作对象名称")
note: Optional[str] = Field(sa_column=Column(Text), description="操作备注")
ip_address: Optional[str] = Field(index=True, description="操作时客户端的IP地址")
create_time: Optional[datetime] = Field(sa_column=Column(
DateTime, nullable=False, index=True, server_default=text('CURRENT_TIMESTAMP')), description="操作时间")
Expand Down
2 changes: 1 addition & 1 deletion src/backend/bisheng/database/models/group_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def get_group_all_resource(cls, group_id: int) -> List[GroupResource]:
"""
with session_getter() as session:
return session.exec(
select(GroupResource).where(GroupResource.group_id == group_id))
select(GroupResource).where(GroupResource.group_id == group_id)).all()

@classmethod
def update_group_resource(cls, group_resources: List[GroupResource]) -> List[GroupResource]:
Expand Down
12 changes: 12 additions & 0 deletions src/backend/bisheng/database/models/user_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,15 @@ def delete_group_admins(cls, group_id: int, admin_ids: List[int]) -> None:
UserGroup.is_group_admin == 1)
session.exec(statement)
session.commit()

@classmethod
def delete_group_all_admin(cls, group_id: int) -> None:
"""
删除用户组下所有的管理员
"""
with session_getter() as session:
statement = delete(UserGroup).where(
UserGroup.group_id == group_id).where(
UserGroup.is_group_admin == 1)
session.exec(statement)
session.commit()
2 changes: 1 addition & 1 deletion src/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default function App() {

// 动态路由根据权限
const router = useMemo(() => {
return getAdminRouter()
// return getAdminRouter()
if (user && ['admin', 'group_admin'].includes(user.role)) return getAdminRouter()
return user?.user_id ? getPrivateRouter(user.web_menu) : null
}, [user])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export default function MessagePanne({ useName, guideWord, loadMore }) {
type = 'separator'
} else if (msg.files?.length) {
type = 'file'
} else if (['tool', 'flow', 'knowledge'].includes(msg.category)) {
// || msg.category === 'processing') { // 项目演示?
} else if (['tool', 'flow', 'knowledge'].includes(msg.category)
|| msg.category === 'processing') { // 项目演示?
type = 'runLog'
} else if (msg.thought) {
type = 'system'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export default function MessageSystem({ data }) {
return <div className="py-1">
<div className={`relative rounded-sm px-6 py-4 border text-sm ${data.category === 'guide' ? 'bg-[#EDEFF6]' : 'bg-slate-50'} ${border[data.category || 'system']}`}>
{logMkdown}
{/* 中英 */}
{data.category === 'report' && <CopyIcon className=" absolute right-4 top-2 cursor-pointer" onClick={(e) => handleCopy(e.target.parentNode)}></CopyIcon>}
{/* {<CopyIcon className=" absolute right-4 top-2 cursor-pointer" onClick={(e) => handleCopy(e.target.parentNode)}></CopyIcon>} */}
</div>
</div>
};
2 changes: 1 addition & 1 deletion src/frontend/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ i18n.use(Backend)
ns: ['bs'],
lng: 'zh', // userLanguage === 'zh' ? userLanguage : 'en', // 除中文即英文
backend: {
loadPath: __APP_ENV__.BASE_URL + '/locales/{{lng}}/{{ns}}.json'
loadPath: __APP_ENV__.BASE_URL + '/locales/{{lng}}/{{ns}}.json?v=' + new Date().getTime()
},
interpolation: {
escapeValue: false // react already safes from xss
Expand Down
1 change: 0 additions & 1 deletion src/frontend/src/layout/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export default function MainLayout() {
<ModelIcon className="h-6 w-6 my-[12px]" /><span className="mx-[14px] max-w-[48px] text-[14px] leading-[48px]">{t('menu.models')}</span>
</NavLink>
}
{/* 中英 */}
{
isMenu('evaluation') &&
<NavLink to='/evaluation' className={`navlink inline-flex rounded-lg w-full px-6 hover:bg-nav-hover h-12 mb-[3.5px]`}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export default function ChatPanne({ customWsHost = '', appendHistory = false, da
</div>
<ChatComponent
form={flowSate.isForm}
// stop={flowSate.isReport || flowSate.isRoom}
stop={flowSate.isReport || flowSate.isRoom}
useName={sendUserName}
guideWord={flow.guide_word}
wsUrl={wsUrl}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/pages/ChatAppPage/components/FileView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FixedSizeList as List, areEqual } from 'react-window';

const SASS_HOST = 'https://bisheng.dataelem.com'
export const checkSassUrl = (url: string) => {
return url.replace(/https?:\/\/[^\/]+/, '')
return url.replace(/https?:\/\/[^\/]+/, __APP_ENV__.BASE_URL)
// location.origin === SASS_HOST ? url.replace(/https?:\/\/[^\/]+/, '') : url;
}
interface Chunk {
Expand Down
25 changes: 13 additions & 12 deletions src/frontend/src/pages/EvaluationPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
EvaluationType,
EvaluationTypeLabelMap,
} from "./types";
import { checkSassUrl } from "../ChatAppPage/components/FileView";

export default function EvaluationPage() {
const navigate = useNavigate();
Expand Down Expand Up @@ -76,7 +77,7 @@ export default function EvaluationPage() {

const handleDownload = async (el) => {
const { url } = await getEvaluationUrlApi(el.result_file_path);
await downloadFile(url, el.file_name);
await downloadFile(checkSassUrl(url), el.file_name);
};

return (
Expand Down Expand Up @@ -162,17 +163,17 @@ export default function EvaluationPage() {
<div className="flex flex-wrap">
{el.result_score
? map(el.result_score, (value, key) => {
return (
<span className="whitespace-nowrap">
{
EvaluationScoreLabelMap[
EvaluationScore[key]
].label
}
:{value}&nbsp;
</span>
);
})
return (
<span className="whitespace-nowrap">
{
EvaluationScoreLabelMap[
EvaluationScore[key]
].label
}
:{value}&nbsp;
</span>
);
})
: "-"}
</div>
</TableCell>
Expand Down
1 change: 0 additions & 1 deletion src/frontend/src/pages/SystemPage/components/EditRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ export default function EditRole({ id, name, groupId, onChange, onBeforeChange }
<Switch checked={form.useMenu.includes(MenuType.MODEL)} onCheckedChange={(bln) => switchDataChange(MenuType.MODEL, 'useMenu', bln)} />
</TableCell>
</TableRow>
{/* 中英 */}
<TableRow>
<TableCell className="font-medium">评测</TableCell>
<TableCell className="text-right">
Expand Down

0 comments on commit 157ae72

Please sign in to comment.