From ce0d46ec1ad096ce5a544011fdba7775301ada73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=8D=9A?= <1016318004@qq.com> Date: Wed, 22 Jan 2025 16:00:54 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9chat=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E4=BD=93=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/entities/request_data.py | 4 ++-- apps/entities/task.py | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/apps/entities/request_data.py b/apps/entities/request_data.py index bdb8693a..5e5e442e 100644 --- a/apps/entities/request_data.py +++ b/apps/entities/request_data.py @@ -6,7 +6,7 @@ from typing import Optional from pydantic import BaseModel, Field -from apps.entities.task import RequestDataPlugin +from apps.entities.task import RequestDataApp from apps.entities.flow import PositionItem,FlowItem,NodeItem,EdgeItem class RequestDataFeatures(BaseModel): @@ -24,7 +24,7 @@ class RequestData(BaseModel): group_id: str language: str = Field(default="zh", description="语言") files: list[str] = Field(default=[]) - plugins: list[RequestDataPlugin] = Field(default=[]) + apps: list[RequestDataApp] = Field(default=[]) features: RequestDataFeatures = Field(description="消息功能设置") diff --git a/apps/entities/task.py b/apps/entities/task.py index d294492f..3b9af5b0 100644 --- a/apps/entities/task.py +++ b/apps/entities/task.py @@ -54,13 +54,11 @@ class TaskBlock(BaseModel): new_context: list[str] = Field(description="Flow的执行信息(增量ID)", default=[]) -class RequestDataPlugin(BaseModel): - """POST /api/chat的plugins字段数据""" - - plugin_id: str = Field(description="插件ID") - flow_id: str = Field(description="Flow ID") +class RequestDataApp(BaseModel): + """模型对话中包含的app信息""" + app_id: str = Field(description="应用ID", alias="appId") + flow_id: str = Field(description="Flow ID", alias="flowId") params: dict[str, Any] = Field(description="插件参数") - auth: dict[str, Any] = Field(description="插件鉴权信息") class Task(BaseModel): @@ -76,3 +74,4 @@ class Task(BaseModel): state: Optional[ExecutorState] = Field(description="Flow的状态", default=None) ended: bool = False updated_at: float = Field(default_factory=lambda: round(datetime.now(tz=timezone.utc).timestamp(), 3)) + -- Gitee From 376269cf6ecc6a2ddd3351c96b610c7a216f4d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=8D=9A?= <1016318004@qq.com> Date: Wed, 22 Jan 2025 16:17:11 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=8D=E7=94=A8?= =?UTF-8?q?=E7=9A=84conversation=E7=9B=B8=E5=85=B3=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=92=8Capi=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/entities/response_data.py | 2 ++ apps/routers/conversation.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/entities/response_data.py b/apps/entities/response_data.py index fcb9c4d8..817069ab 100644 --- a/apps/entities/response_data.py +++ b/apps/entities/response_data.py @@ -105,6 +105,8 @@ class ConversationListItem(BaseModel): title: str doc_count: int = Field(alias="docCount") created_time: str = Field(alias="createdTime") + app_id: str = Field(alias="appId") + is_debug: bool = Field(alias="isDebug") class ConversationListMsg(BaseModel): """GET /api/conversation Result数据结构""" diff --git a/apps/routers/conversation.py b/apps/routers/conversation.py index 83e8ef72..da752e0f 100644 --- a/apps/routers/conversation.py +++ b/apps/routers/conversation.py @@ -77,6 +77,8 @@ async def get_conversation_list(user_sub: Annotated[str, Depends(get_user)]): # title=conv.title, docCount=await DocumentManager.get_doc_count(user_sub, conv.id), createdTime=datetime.fromtimestamp(conv.created_at, tz=pytz.timezone("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S"), + appId=conv.app_id, + isDebug=conv.is_debug, ) for conv in conversations ] @@ -96,6 +98,8 @@ async def get_conversation_list(user_sub: Annotated[str, Depends(get_user)]): # title=new_conv.title, docCount=0, createdTime=datetime.fromtimestamp(new_conv.created_at, tz=pytz.timezone("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S"), + appId=new_conv.app_id, + isDebug=new_conv.is_debug, )) return JSONResponse(status_code=status.HTTP_200_OK, @@ -109,8 +113,13 @@ async def get_conversation_list(user_sub: Annotated[str, Depends(get_user)]): # @router.post("", dependencies=[Depends(verify_csrf_token)], response_model=AddConversationRsp) -async def add_conversation(user_sub: Annotated[str, Depends(get_user)]): # noqa: ANN201 +async def add_conversation( + user_sub: Annotated[str, Depends(get_user)], + appId: Annotated[Optional[str], Query()] = None, + isDebug: Annotated[Optional[bool], Query()] = None, +): """手动创建新对话""" + # TODO(@fangbo):增加is_debug和appId作用 conversations = await ConversationManager.get_conversation_by_user_sub(user_sub) # 尝试创建新对话 try: @@ -177,6 +186,8 @@ async def update_conversation( # noqa: ANN201 title=conv.title, docCount=await DocumentManager.get_doc_count(user_sub, conv.id), createdTime=datetime.fromtimestamp(conv.created_at, tz=pytz.timezone("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S"), + appId=conv.app_id, + isDebug=conv.is_debug, ), ).model_dump(exclude_none=True, by_alias=True), ) -- Gitee From e3212c01d6a6854c8aae1620c521f1c96804c06e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=8D=9A?= <1016318004@qq.com> Date: Wed, 22 Jan 2025 16:29:21 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D=E7=9A=84=E6=97=B6=E5=80=99=E7=9A=84appId?= =?UTF-8?q?=E5=92=8CisDebug=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/routers/conversation.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/apps/routers/conversation.py b/apps/routers/conversation.py index da752e0f..55262d3d 100644 --- a/apps/routers/conversation.py +++ b/apps/routers/conversation.py @@ -43,7 +43,12 @@ router = APIRouter( ) -async def create_new_conversation(user_sub: str, conv_list: list[Conversation]) -> Optional[Conversation]: +async def create_new_conversation( + user_sub: str, + conv_list: list[Conversation], + app_id: str = "", + is_debug: bool = False, +) -> Optional[Conversation]: """判断并创建新对话""" create_new = False if not conv_list: @@ -115,15 +120,17 @@ async def get_conversation_list(user_sub: Annotated[str, Depends(get_user)]): # @router.post("", dependencies=[Depends(verify_csrf_token)], response_model=AddConversationRsp) async def add_conversation( user_sub: Annotated[str, Depends(get_user)], - appId: Annotated[Optional[str], Query()] = None, - isDebug: Annotated[Optional[bool], Query()] = None, + appId: Optional[str] = None, # noqa: N803 + isDebug: Optional[bool] = None, # noqa: N803 ): """手动创建新对话""" - # TODO(@fangbo):增加is_debug和appId作用 conversations = await ConversationManager.get_conversation_by_user_sub(user_sub) # 尝试创建新对话 try: - new_conv = await create_new_conversation(user_sub, conversations) + app_id = appId if appId else "" + is_debug = isDebug if isDebug is not None else False + new_conv = await create_new_conversation(user_sub, conversations, + app_id=app_id, is_debug=is_debug) except RuntimeError as e: return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=ResponseData( code=status.HTTP_500_INTERNAL_SERVER_ERROR, -- Gitee From ebfbb282e0fc3923d91ebff2ccd2f912b68bac1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=8D=9A?= <1016318004@qq.com> Date: Thu, 23 Jan 2025 10:25:08 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0app=5Fid=E5=90=88?= =?UTF-8?q?=E6=B3=95=E6=80=A7=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/routers/conversation.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apps/routers/conversation.py b/apps/routers/conversation.py index 55262d3d..80f36d7e 100644 --- a/apps/routers/conversation.py +++ b/apps/routers/conversation.py @@ -33,6 +33,7 @@ from apps.manager import ( DocumentManager, RecordManager, ) +from apps.manager.application import AppManager router = APIRouter( prefix="/api/conversation", @@ -61,7 +62,12 @@ async def create_new_conversation( # 新建对话 if create_new: - new_conv = await ConversationManager.add_conversation_by_user_sub(user_sub, app_id, is_debug=False) + if not AppManager.validate_user_app_access(user_sub, app_id): + err = "Invalid app_id." + raise RuntimeError(err) + new_conv = await ConversationManager.add_conversation_by_user_sub(user_sub, + app_id=app_id, + is_debug=is_debug) if not new_conv: err = "Create new conversation failed." raise RuntimeError(err) -- Gitee