Skip to content

Commit 54e16ac

Browse files
committed
feat: add application enable flag and application IDs to application model
1 parent fe004c2 commit 54e16ac

File tree

13 files changed

+581
-35
lines changed

13 files changed

+581
-35
lines changed

apps/application/chat_pipeline/step/chat_step/i_chat_step.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class InstanceSerializer(serializers.Serializer):
8888
mcp_source = serializers.CharField(label="MCP Source", required=False, default="referencing")
8989
tool_enable = serializers.BooleanField(label="工具是否启用", required=False, default=False)
9090
tool_ids = serializers.JSONField(label="工具ID列表", required=False, default=list)
91+
application_enable = serializers.BooleanField(label="应用是否启用", required=False, default=False)
92+
application_ids = serializers.JSONField(label="应用ID列表", required=False, default=list)
9193
mcp_output_enable = serializers.BooleanField(label="MCP输出是否启用", required=False, default=True)
9294

9395
def is_valid(self, *, raise_exception=False):
@@ -115,6 +117,6 @@ def execute(self, message_list: List[BaseMessage],
115117
padding_problem_text: str = None, stream: bool = True, chat_user_id=None, chat_user_type=None,
116118
no_references_setting=None, model_params_setting=None, model_setting=None,
117119
mcp_enable=False, mcp_tool_ids=None, mcp_servers='', mcp_source="referencing",
118-
tool_enable=False, tool_ids=None, mcp_output_enable=True,
120+
tool_enable=False, tool_ids=None, application_enable=None, application_ids=None, mcp_output_enable=True,
119121
**kwargs):
120122
pass

apps/application/chat_pipeline/step/chat_step/impl/base_chat_step.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
@desc: 对话step Base实现
88
"""
99
import json
10-
import os
1110
import time
1211
import traceback
13-
import uuid_utils.compat as uuid
1412
from typing import List
1513

14+
import uuid_utils.compat as uuid
1615
from django.db.models import QuerySet
1716
from django.http import StreamingHttpResponse
1817
from django.utils.translation import gettext as _
@@ -26,11 +25,10 @@
2625
from application.chat_pipeline.pipeline_manage import PipelineManage
2726
from application.chat_pipeline.step.chat_step.i_chat_step import IChatStep, PostResponseHandler
2827
from application.flow.tools import Reasoning, mcp_response_generator
29-
from application.models import ApplicationChatUserStats, ChatUserType
28+
from application.models import ApplicationChatUserStats, ChatUserType, Application, ApplicationApiKey
3029
from common.utils.logger import maxkb_logger
3130
from common.utils.rsa_util import rsa_long_decrypt
3231
from common.utils.tool_code import ToolExecutor
33-
from maxkb.const import CONFIG
3432
from models_provider.tools import get_model_instance_by_model_workspace_id
3533
from 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-
6360
def 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)

apps/application/flow/step_node/ai_chat_step_node/i_chat_node.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class ChatNodeSerializer(serializers.Serializer):
4242
tool_enable = serializers.BooleanField(required=False, default=False, label=_("Whether to enable tools"))
4343
tool_ids = serializers.ListField(child=serializers.UUIDField(), required=False, allow_empty=True,
4444
label=_("Tool IDs"), )
45+
application_enable = serializers.BooleanField(required=False, default=False, label=_("Whether to enable apps"))
46+
application_ids = serializers.ListField(child=serializers.UUIDField(), required=False, allow_empty=True,
47+
label=_("App IDs"), )
4548
mcp_output_enable = serializers.BooleanField(required=False, default=True, label=_("Whether to enable MCP output"))
4649

4750

@@ -73,6 +76,8 @@ def execute(self, model_id, system, prompt, dialogue_number, history_chat_record
7376
mcp_source=None,
7477
tool_enable=False,
7578
tool_ids=None,
79+
application_enable=False,
80+
application_ids=None,
7681
mcp_output_enable=True,
7782
**kwargs) -> NodeResult:
7883
pass

apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
@desc:
88
"""
99
import json
10-
import os
1110
import re
1211
import time
1312
from functools import reduce
@@ -20,9 +19,9 @@
2019
from application.flow.i_step_node import NodeResult, INode
2120
from application.flow.step_node.ai_chat_step_node.i_chat_node import IChatNode
2221
from application.flow.tools import Reasoning, mcp_response_generator
22+
from application.models import Application
2323
from common.utils.rsa_util import rsa_long_decrypt
2424
from common.utils.tool_code import ToolExecutor
25-
from maxkb.const import CONFIG
2625
from models_provider.models import Model
2726
from models_provider.tools import get_model_credential, get_model_instance_by_model_workspace_id
2827
from tools.models import Tool
@@ -160,6 +159,8 @@ def execute(self, model_id, system, prompt, dialogue_number, history_chat_record
160159
mcp_source=None,
161160
tool_enable=False,
162161
tool_ids=None,
162+
application_enable=False,
163+
application_ids=None,
163164
mcp_output_enable=True,
164165
**kwargs) -> NodeResult:
165166
if dialogue_type is None:
@@ -186,7 +187,8 @@ def execute(self, model_id, system, prompt, dialogue_number, history_chat_record
186187

187188
# 处理 MCP 请求
188189
mcp_result = self._handle_mcp_request(
189-
mcp_enable, tool_enable, mcp_source, mcp_servers, mcp_tool_id, mcp_tool_ids, tool_ids, mcp_output_enable,
190+
mcp_enable, tool_enable, mcp_source, mcp_servers, mcp_tool_id, mcp_tool_ids, tool_ids, application_enable,
191+
application_ids, mcp_output_enable,
190192
chat_model, message_list, history_message, question
191193
)
192194
if mcp_result:
@@ -208,8 +210,9 @@ def execute(self, model_id, system, prompt, dialogue_number, history_chat_record
208210
_write_context=write_context)
209211

210212
def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers, mcp_tool_id, mcp_tool_ids, tool_ids,
213+
application_enable, application_ids,
211214
mcp_output_enable, chat_model, message_list, history_message, question):
212-
if not mcp_enable and not tool_enable:
215+
if not mcp_enable and not tool_enable and not application_enable:
213216
return None
214217

215218
mcp_servers_config = {}
@@ -245,10 +248,19 @@ def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers,
245248
params = json.loads(rsa_long_decrypt(tool.init_params))
246249
else:
247250
params = {}
248-
tool_config = executor.get_tool_mcp_config(tool.code, params)
251+
tool_config = executor.get_tool_mcp_config(tool.code, params, tool.name, tool.desc)
249252

250253
mcp_servers_config[str(tool.id)] = tool_config
251254

255+
if application_enable:
256+
if application_ids and len(application_ids) > 0:
257+
self.context['application_ids'] = application_ids
258+
for application_id in application_ids:
259+
app = QuerySet(Application).filter(id=application_id).first()
260+
executor = ToolExecutor()
261+
app_config = executor.get_app_mcp_config(app.id, app.name, app.desc)
262+
mcp_servers_config[str(app.id)] = app_config
263+
252264
if len(mcp_servers_config) > 0:
253265
r = mcp_response_generator(chat_model, message_list, json.dumps(mcp_servers_config), mcp_output_enable)
254266
return NodeResult(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 5.2.9 on 2025-12-16 08:02
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('application', '0003_application_stt_model_params_setting_and_more'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='application',
15+
name='application_enable',
16+
field=models.BooleanField(default=False, verbose_name='应用是否启用'),
17+
),
18+
migrations.AddField(
19+
model_name='application',
20+
name='application_ids',
21+
field=models.JSONField(default=list, verbose_name='应用ID列表'),
22+
),
23+
]

apps/application/models/application.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class Application(AppModelMixin):
100100
mcp_source = models.CharField(verbose_name="MCP Source", max_length=20, default="referencing")
101101
tool_enable = models.BooleanField(verbose_name="工具是否启用", default=False)
102102
tool_ids = models.JSONField(verbose_name="工具ID列表", default=list)
103+
application_enable = models.BooleanField(verbose_name="应用是否启用", default=False)
104+
application_ids = models.JSONField(verbose_name="应用ID列表", default=list)
103105
mcp_output_enable = models.BooleanField(verbose_name="MCP输出是否启用", default=True)
104106

105107
@staticmethod

apps/application/serializers/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ def edit(self, instance: Dict, with_valid=True):
857857
'api_key_is_active', 'icon', 'work_flow', 'model_params_setting', 'tts_model_params_setting',
858858
'stt_model_params_setting',
859859
'mcp_enable', 'mcp_tool_ids', 'mcp_servers', 'mcp_source', 'tool_enable', 'tool_ids',
860-
'mcp_output_enable',
860+
'mcp_output_enable', 'application_enable', 'application_ids',
861861
'problem_optimization_prompt', 'clean_time', 'folder_id']
862862
for update_key in update_keys:
863863
if update_key in instance and instance.get(update_key) is not None:

apps/application/serializers/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ def to_base_pipeline_manage_params(self):
162162
'mcp_source': self.application.mcp_source,
163163
'tool_enable': self.application.tool_enable,
164164
'tool_ids': self.application.tool_ids,
165+
'application_enable': self.application.application_enable,
166+
'application_ids': self.application.application_ids,
165167
'mcp_output_enable': self.application.mcp_output_enable,
166168
}
167169

0 commit comments

Comments
 (0)