77 @desc: 对话step Base实现
88"""
99import json
10- import os
1110import time
1211import traceback
13- import uuid_utils .compat as uuid
1412from typing import List
1513
14+ import uuid_utils .compat as uuid
1615from django .db .models import QuerySet
1716from django .http import StreamingHttpResponse
1817from django .utils .translation import gettext as _
2625from application .chat_pipeline .pipeline_manage import PipelineManage
2726from application .chat_pipeline .step .chat_step .i_chat_step import IChatStep , PostResponseHandler
2827from application .flow .tools import Reasoning , mcp_response_generator
29- from application .models import ApplicationChatUserStats , ChatUserType
28+ from application .models import ApplicationChatUserStats , ChatUserType , Application , ApplicationApiKey
3029from common .utils .logger import maxkb_logger
3130from common .utils .rsa_util import rsa_long_decrypt
3231from common .utils .tool_code import ToolExecutor
33- from maxkb .const import CONFIG
3432from models_provider .tools import get_model_instance_by_model_workspace_id
3533from tools .models import Tool
3634
@@ -59,7 +57,6 @@ def write_context(step, manage, request_token, response_token, all_text):
5957 manage .context ['answer_tokens' ] = manage .context ['answer_tokens' ] + response_token
6058
6159
62-
6360def event_content (response ,
6461 chat_id ,
6562 chat_record_id ,
@@ -182,6 +179,8 @@ def execute(self, message_list: List[BaseMessage],
182179 mcp_source = "referencing" ,
183180 tool_enable = False ,
184181 tool_ids = None ,
182+ application_enable = False ,
183+ application_ids = None ,
185184 mcp_output_enable = True ,
186185 ** kwargs ):
187186 chat_model = get_model_instance_by_model_workspace_id (model_id , workspace_id ,
@@ -193,13 +192,15 @@ def execute(self, message_list: List[BaseMessage],
193192 no_references_setting ,
194193 model_setting ,
195194 mcp_enable , mcp_tool_ids , mcp_servers , mcp_source , tool_enable , tool_ids ,
195+ application_enable , application_ids ,
196196 mcp_output_enable )
197197 else :
198198 return self .execute_block (message_list , chat_id , problem_text , post_response_handler , chat_model ,
199199 paragraph_list ,
200200 manage , padding_problem_text , chat_user_id , chat_user_type , no_references_setting ,
201201 model_setting ,
202202 mcp_enable , mcp_tool_ids , mcp_servers , mcp_source , tool_enable , tool_ids ,
203+ application_enable , application_ids ,
203204 mcp_output_enable )
204205
205206 def get_details (self , manage , ** kwargs ):
@@ -225,8 +226,9 @@ def reset_message_list(message_list: List[BaseMessage], answer_text):
225226 return result
226227
227228 def _handle_mcp_request (self , mcp_enable , tool_enable , mcp_source , mcp_servers , mcp_tool_ids , tool_ids ,
229+ application_enable , application_ids ,
228230 mcp_output_enable , chat_model , message_list ):
229- if not mcp_enable and not tool_enable :
231+ if not mcp_enable and not tool_enable and not application_enable :
230232 return None
231233
232234 mcp_servers_config = {}
@@ -258,10 +260,25 @@ def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers,
258260 params = json .loads (rsa_long_decrypt (tool .init_params ))
259261 else :
260262 params = {}
261- tool_config = executor .get_tool_mcp_config (tool .code , params )
263+ tool_config = executor .get_tool_mcp_config (tool .code , params , tool . name , tool . desc )
262264
263265 mcp_servers_config [str (tool .id )] = tool_config
264266
267+ if application_enable :
268+ if application_ids and len (application_ids ) > 0 :
269+ self .context ['application_ids' ] = application_ids
270+ for application_id in application_ids :
271+ app = QuerySet (Application ).filter (id = application_id ).first ()
272+ app_key = QuerySet (ApplicationApiKey ).filter (application_id = application_id , is_active = True ).first ()
273+ # TODO 处理api
274+ if app_key is not None :
275+ api_key = app_key .secret_key
276+ else :
277+ continue
278+ executor = ToolExecutor ()
279+ app_config = executor .get_app_mcp_config (api_key , app .name , app .desc )
280+ mcp_servers_config [str (app .id )] = app_config
281+
265282 if len (mcp_servers_config ) > 0 :
266283 return mcp_response_generator (chat_model , message_list , json .dumps (mcp_servers_config ), mcp_output_enable )
267284
@@ -278,6 +295,8 @@ def get_stream_result(self, message_list: List[BaseMessage],
278295 mcp_source = "referencing" ,
279296 tool_enable = False ,
280297 tool_ids = None ,
298+ application_enable = False ,
299+ application_ids = None ,
281300 mcp_output_enable = True ):
282301 if paragraph_list is None :
283302 paragraph_list = []
@@ -296,7 +315,8 @@ def get_stream_result(self, message_list: List[BaseMessage],
296315 else :
297316 # 处理 MCP 请求
298317 mcp_result = self ._handle_mcp_request (
299- mcp_enable , tool_enable , mcp_source , mcp_servers , mcp_tool_ids , tool_ids , mcp_output_enable , chat_model ,
318+ mcp_enable , tool_enable , mcp_source , mcp_servers , mcp_tool_ids , tool_ids , application_enable ,
319+ application_ids , mcp_output_enable , chat_model ,
300320 message_list ,
301321 )
302322 if mcp_result :
@@ -320,10 +340,13 @@ def execute_stream(self, message_list: List[BaseMessage],
320340 mcp_source = "referencing" ,
321341 tool_enable = False ,
322342 tool_ids = None ,
343+ application_enable = False ,
344+ application_ids = None ,
323345 mcp_output_enable = True ):
324346 chat_result , is_ai_chat = self .get_stream_result (message_list , chat_model , paragraph_list ,
325347 no_references_setting , problem_text , mcp_enable , mcp_tool_ids ,
326348 mcp_servers , mcp_source , tool_enable , tool_ids ,
349+ application_enable , application_ids ,
327350 mcp_output_enable )
328351 chat_record_id = uuid .uuid7 ()
329352 r = StreamingHttpResponse (
@@ -347,6 +370,8 @@ def get_block_result(self, message_list: List[BaseMessage],
347370 mcp_source = "referencing" ,
348371 tool_enable = False ,
349372 tool_ids = None ,
373+ application_enable = False ,
374+ application_ids = None ,
350375 mcp_output_enable = True
351376 ):
352377 if paragraph_list is None :
@@ -365,7 +390,8 @@ def get_block_result(self, message_list: List[BaseMessage],
365390 else :
366391 # 处理 MCP 请求
367392 mcp_result = self ._handle_mcp_request (
368- mcp_enable , tool_enable , mcp_source , mcp_servers , mcp_tool_ids , tool_ids , mcp_output_enable ,
393+ mcp_enable , tool_enable , mcp_source , mcp_servers , mcp_tool_ids , tool_ids , application_enable ,
394+ application_ids , mcp_output_enable ,
369395 chat_model , message_list ,
370396 )
371397 if mcp_result :
@@ -388,6 +414,8 @@ def execute_block(self, message_list: List[BaseMessage],
388414 mcp_source = "referencing" ,
389415 tool_enable = False ,
390416 tool_ids = None ,
417+ application_enable = False ,
418+ application_ids = None ,
391419 mcp_output_enable = True ):
392420 reasoning_content_enable = model_setting .get ('reasoning_content_enable' , False )
393421 reasoning_content_start = model_setting .get ('reasoning_content_start' , '<think>' )
@@ -400,7 +428,8 @@ def execute_block(self, message_list: List[BaseMessage],
400428 chat_result , is_ai_chat = self .get_block_result (message_list , chat_model , paragraph_list ,
401429 no_references_setting , problem_text , mcp_enable ,
402430 mcp_tool_ids , mcp_servers , mcp_source , tool_enable ,
403- tool_ids , mcp_output_enable )
431+ tool_ids , application_enable , application_ids ,
432+ mcp_output_enable )
404433 if is_ai_chat :
405434 request_token = chat_model .get_num_tokens_from_messages (message_list )
406435 response_token = chat_model .get_num_tokens (chat_result .content )
0 commit comments