|
13 | 13 |
|
14 | 14 | from apps.chat.api.chat import create_chat, question_answer_inner |
15 | 15 | from apps.chat.models.chat_model import ChatMcp, CreateChat, ChatStart, McpQuestion, McpAssistant, ChatQuestion, \ |
16 | | - ChatFinishStep, McpDs |
| 16 | + ChatFinishStep, McpDs, ChatToken |
17 | 17 | from apps.datasource.crud.datasource import get_datasource_list |
18 | 18 | from apps.system.crud.user import authenticate, user_ws_options |
19 | 19 | from apps.system.crud.user import get_db_user |
|
34 | 34 | router = APIRouter(tags=["mcp"], prefix="/mcp") |
35 | 35 |
|
36 | 36 |
|
37 | | -# @router.post("/access_token", operation_id="access_token") |
38 | | -# def local_login( |
39 | | -# session: SessionDep, |
40 | | -# form_data: Annotated[OAuth2PasswordRequestForm, Depends()] |
41 | | -# ) -> Token: |
42 | | -# user = authenticate(session=session, account=form_data.username, password=form_data.password) |
43 | | -# if not user: |
44 | | -# raise HTTPException(status_code=400, detail="Incorrect account or password") |
45 | | -# access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) |
46 | | -# user_dict = user.to_dict() |
47 | | -# return Token(access_token=create_access_token( |
48 | | -# user_dict, expires_delta=access_token_expires |
49 | | -# )) |
| 37 | +@router.post("/access_token", operation_id="access_token") |
| 38 | +async def access_token(session: SessionDep, chat: ChatToken): |
| 39 | + user: BaseUserDTO = authenticate(session=session, account=chat.username, password=chat.password) |
| 40 | + if not user: |
| 41 | + raise HTTPException(status_code=400, detail="Incorrect account or password") |
| 42 | + |
| 43 | + if not user.oid or user.oid == 0: |
| 44 | + raise HTTPException(status_code=400, detail="No associated workspace, Please contact the administrator") |
| 45 | + access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) |
| 46 | + user_dict = user.to_dict() |
| 47 | + t = Token(access_token=create_access_token( |
| 48 | + user_dict, expires_delta=access_token_expires |
| 49 | + )) |
| 50 | + # c = create_chat(session, user, CreateChat(origin=1), False) |
| 51 | + return {"access_token": t.access_token} |
50 | 52 |
|
51 | 53 |
|
52 | 54 | def get_user(session: SessionDep, token: str): |
@@ -82,20 +84,36 @@ def get_user(session: SessionDep, token: str): |
82 | 84 |
|
83 | 85 |
|
84 | 86 | @router.post("/mcp_start", operation_id="mcp_start") |
85 | | -async def mcp_start(session: SessionDep, chat: ChatStart): |
86 | | - user: BaseUserDTO = authenticate(session=session, account=chat.username, password=chat.password) |
87 | | - if not user: |
88 | | - raise HTTPException(status_code=400, detail="Incorrect account or password") |
| 87 | +async def mcp_start(session: SessionDep, trans: Trans, chat: ChatStart): |
| 88 | + res_token = None |
| 89 | + user = None |
| 90 | + if chat.token: |
| 91 | + res_token = chat.token |
| 92 | + user = get_user(session, chat.token) |
| 93 | + else: |
| 94 | + user = authenticate(session=session, account=chat.username, password=chat.password) |
| 95 | + if not user: |
| 96 | + raise HTTPException(status_code=400, detail="Incorrect account or password") |
| 97 | + |
| 98 | + if not user.oid or user.oid == 0: |
| 99 | + raise HTTPException(status_code=400, detail="No associated workspace, Please contact the administrator") |
| 100 | + access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) |
| 101 | + user_dict = user.to_dict() |
| 102 | + t = Token(access_token=create_access_token( |
| 103 | + user_dict, expires_delta=access_token_expires |
| 104 | + )) |
| 105 | + res_token = t.access_token |
| 106 | + |
| 107 | + if chat.oid: |
| 108 | + w_list = await user_ws_options(session, user.id, trans) |
| 109 | + oid_list = [item.id for item in w_list] |
| 110 | + if int(chat.oid) not in oid_list: |
| 111 | + raise HTTPException(status_code=400, detail="The current user is not in the selected workspace") |
| 112 | + |
| 113 | + user.oid = int(chat.oid) |
89 | 114 |
|
90 | | - if not user.oid or user.oid == 0: |
91 | | - raise HTTPException(status_code=400, detail="No associated workspace, Please contact the administrator") |
92 | | - access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) |
93 | | - user_dict = user.to_dict() |
94 | | - t = Token(access_token=create_access_token( |
95 | | - user_dict, expires_delta=access_token_expires |
96 | | - )) |
97 | 115 | c = create_chat(session, user, CreateChat(origin=1), False) |
98 | | - return {"access_token": t.access_token, "chat_id": c.id} |
| 116 | + return {"access_token": res_token, "chat_id": c.id} |
99 | 117 |
|
100 | 118 |
|
101 | 119 | @router.post("/mcp_ws_list", operation_id="mcp_ws_list") |
@@ -139,13 +157,13 @@ async def mcp_question(session: SessionDep, trans: Trans, chat: McpQuestion): |
139 | 157 | lang = chat.lang |
140 | 158 | if lang in ["zh-CN", "zh-TW", "en", "ko-KR"]: |
141 | 159 | session_user.language = lang |
142 | | - if chat.oid: |
143 | | - w_list = await user_ws_options(session, session_user.id, trans) |
144 | | - oid_list = [item.id for item in w_list] |
145 | | - if int(chat.oid) not in oid_list: |
146 | | - raise HTTPException(status_code=400, detail="The current user is not in the selected workspace") |
147 | | - |
148 | | - session_user.oid = int(chat.oid) |
| 160 | + # if chat.oid: |
| 161 | + # w_list = await user_ws_options(session, session_user.id, trans) |
| 162 | + # oid_list = [item.id for item in w_list] |
| 163 | + # if int(chat.oid) not in oid_list: |
| 164 | + # raise HTTPException(status_code=400, detail="The current user is not in the selected workspace") |
| 165 | + # |
| 166 | + # session_user.oid = int(chat.oid) |
149 | 167 | ds_id: Optional[int] = None |
150 | 168 | if chat.datasource_id: |
151 | 169 | if isinstance(chat.datasource_id, str): |
|
0 commit comments