Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 5.2.15 on 2026-07-29 02:42

from django.db import migrations, models


def forwards(apps, schema_editor):
Application = apps.get_model("application", "Application")
ResourceMapping = apps.get_model("system_manage", "ResourceMapping")
ApplicationVersion = apps.get_model("application", "ApplicationVersion")

APPLICATION = "APPLICATION"
KNOWLEDGE = "KNOWLEDGE"
SIMPLE = "SIMPLE"
db_alias = schema_editor.connection.alias
simple_application_ids = {
str(app_id)
for app_id in Application.objects.using(db_alias)
.filter(type=SIMPLE)
.values_list("id", flat=True)
}
mapping = {}
qs = (
ResourceMapping.objects.using(db_alias)
.filter(source_type=APPLICATION, target_type=KNOWLEDGE)
.values_list("source_id", "target_id")
)
for source_id, target_id in qs.iterator():
if source_id in simple_application_ids:
mapping.setdefault(source_id, []).append(target_id)
mapping = {k: list(dict.fromkeys(v)) for k, v in mapping.items()}

updates = []
for obj in ApplicationVersion.objects.using(db_alias).iterator():
app_id = str(obj.application_id)
if app_id not in simple_application_ids:
continue
knowledge_ids = mapping.get(app_id)
if knowledge_ids:
obj.knowledge_ids = knowledge_ids
updates.append(obj)
if updates:
ApplicationVersion.objects.using(db_alias).bulk_update(
updates, ["knowledge_ids"], batch_size=500
)


class Migration(migrations.Migration):

dependencies = [
('application', '0013_application_long_term_enable_and_more'),
('system_manage', '0005_resourcemapping'),
]

operations = [
migrations.AddField(
model_name='applicationversion',
name='knowledge_ids',
field=models.JSONField(default=list, verbose_name='数据集id列表'),
),
migrations.RunPython(forwards, migrations.RunPython.noop),
]
1 change: 1 addition & 0 deletions apps/application/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class ApplicationVersion(AppModelMixin):
long_term_model_params_setting = models.JSONField(verbose_name="长期记忆模型参数相关设置", default=dict)
long_term_trigger_type = models.CharField(verbose_name='长期记忆触发类型', default='ROUND')
long_term_trigger_setting = models.JSONField(verbose_name='长期记忆触发配置', default=dict)
knowledge_ids = models.JSONField(verbose_name="数据集id列表", default=list)

class Meta:
db_table = "application_version"
28 changes: 17 additions & 11 deletions apps/application/serializers/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_bound_tool_ids(instance: Dict) -> List[str]:
"""
tool_ids = set()
for key in ("tool_ids", "skill_tool_ids", "mcp_tool_ids"):
for tool_id in (instance.get(key) or []):
for tool_id in instance.get(key) or []:
tool_ids.add(str(tool_id))
if instance.get("mcp_tool_id"):
tool_ids.add(str(instance.get("mcp_tool_id")))
Expand All @@ -96,7 +96,7 @@ def collect(node_data):
if node_data.get(key):
tool_ids.add(str(node_data.get(key)))
for key in ("mcp_tool_ids", "tool_ids", "skill_tool_ids"):
for tool_id in (node_data.get(key) or []):
for tool_id in node_data.get(key) or []:
tool_ids.add(str(tool_id))

_walk_workflow_nodes(instance.get("work_flow"), collect)
Expand All @@ -109,11 +109,11 @@ def get_bound_application_ids(instance: Dict) -> List[str]:
ai-chat-node 的 node_data 包含 application_ids 列表。
"""
application_ids = set()
for app_id in (instance.get("application_ids") or []):
for app_id in instance.get("application_ids") or []:
application_ids.add(str(app_id))

def collect(node_data):
for app_id in (node_data.get("application_ids") or []):
for app_id in node_data.get("application_ids") or []:
application_ids.add(str(app_id))

_walk_workflow_nodes(instance.get("work_flow"), collect)
Expand Down Expand Up @@ -186,7 +186,9 @@ def validate_bound_tool_permissions(user_id: str, workspace_id: str, instance: D
application_ids = get_bound_application_ids(instance)
if application_ids:
authorized_application_ids = set(get_authorized_application_ids(user_id, workspace_id, application_ids))
unauthorized_application_ids = [app_id for app_id in application_ids if app_id not in authorized_application_ids]
unauthorized_application_ids = [
app_id for app_id in application_ids if app_id not in authorized_application_ids
]
if unauthorized_application_ids:
message = lazy_format(
_("No permission to use application(s): {application_ids}"),
Expand Down Expand Up @@ -1268,6 +1270,14 @@ def publish(self, instance, with_valid=True):
workspace_id=workspace_id,
)
self.reset_application_version(work_flow_version, application)
# 如果是简易应用 需要存入 knowledge_ids
if application.type == ApplicationTypeChoices.SIMPLE:
work_flow_version.knowledge_ids = [
str(row.target_id)
for row in QuerySet(ResourceMapping).filter(
source_id=str(application.id), source_type="APPLICATION", target_type="KNOWLEDGE"
)
]
work_flow_version.save()
access_token = hashlib.md5(str(uuid.uuid7()).encode()).hexdigest()[8:24]
application_access_token = QuerySet(ApplicationAccessToken).filter(application_id=application.id).first()
Expand Down Expand Up @@ -1781,9 +1791,7 @@ def batch_delete(self, instance: Dict, with_valid=True):
id_list = instance.get("id_list")
workspace_id = self.data.get("workspace_id")
id_list = list(
QuerySet(Application)
.filter(id__in=id_list, workspace_id=workspace_id)
.values_list("id", flat=True)
QuerySet(Application).filter(id__in=id_list, workspace_id=workspace_id).values_list("id", flat=True)
)
Comment on lines 1793 to 1795

QuerySet(ApplicationVersion).filter(application_id__in=id_list).delete()
Expand Down Expand Up @@ -1842,9 +1850,7 @@ def batch_clean_time(self, instance: Dict, with_valid=True):

class BatchCleanTimeSerializer(BatchSerializer):
clean_time = serializers.IntegerField(required=True, min_value=1, max_value=100000, label=_("Clean time"))
file_clean_time = serializers.IntegerField(
required=True, min_value=1, max_value=100000, label=_("File clean time")
)
file_clean_time = serializers.IntegerField(required=True, min_value=1, max_value=100000, label=_("File clean time"))

def is_valid(self, *, model=None, raise_exception=False):
super().is_valid(model=model, raise_exception=True)
Expand Down
28 changes: 19 additions & 9 deletions apps/chat/serializers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,16 @@ def re_open_chat(self, chat_id: str):
return self.re_open_chat_work_flow(chat_id, application)

def re_open_chat_simple(self, chat_id, application):
# 数据集id列表
knowledge_id_list = [str(row.target_id) for row in
QuerySet(ResourceMapping).filter(source_id=str(application.id),
source_type='APPLICATION',
target_type='KNOWLEDGE')]
if self.data.get('debug'):
# 数据集id列表
knowledge_id_list = [str(row.target_id) for row in
QuerySet(ResourceMapping).filter(source_id=str(application.id),
source_type='APPLICATION',
target_type='KNOWLEDGE')]
else:
application_version = QuerySet(ApplicationVersion).filter(application_id=application.id).order_by(
'-create_time')[0:1].first()
knowledge_id_list = application_version.knowledge_ids

# 需要排除的文档
exclude_document_id_list = [str(document.id) for document in
Expand Down Expand Up @@ -584,10 +589,15 @@ def open_simple(self, application):
ip_address = self.data.get("ip_address")
source = self.data.get("source")
debug = self.data.get("debug")
knowledge_id_list = [str(row.target_id) for row in
QuerySet(ResourceMapping).filter(source_id=str(application_id),
source_type='APPLICATION',
target_type='KNOWLEDGE')]
if debug:
knowledge_id_list = [str(row.target_id) for row in
QuerySet(ResourceMapping).filter(source_id=str(application_id),
source_type='APPLICATION',
target_type='KNOWLEDGE')]
else:
application_version = QuerySet(ApplicationVersion).filter(application_id=application_id).order_by(
'-create_time')[0:1].first()
knowledge_id_list = application_version.knowledge_ids
Comment on lines +598 to +600

chat_id = str(uuid.uuid7())
ChatInfo(chat_id, chat_user_id, chat_user_type, ip_address, source, knowledge_id_list,
Expand Down
Loading