Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

add cnt script and one more example (#28272)

Asuka Minato108bcbeb7c36
diff --git a/.github/workflows/autofix.yml b/.github/workflows/autofix.yml
index 2ce8a09a7d2..81392a97346 100644
--- a/.github/workflows/autofix.yml
+++ b/.github/workflows/autofix.yml
@@ -28,6 +28,11 @@ jobs:
# Format code
uv run ruff format ..
+ - name: count migration progress
+ run: |
+ cd api
+ ./cnt_base.sh
+
- name: ast-grep
run: |
uvx --from ast-grep-cli sg --pattern 'db.session.query($WHATEVER).filter($HERE)' --rewrite 'db.session.query($WHATEVER).where($HERE)' -l py --update-all
diff --git a/api/cnt_base.sh b/api/cnt_base.sh
new file mode 100755
index 00000000000..9e407f3584f
--- /dev/null
+++ b/api/cnt_base.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+set -euxo pipefail
+
+for pattern in "Base" "TypeBase"; do
+ printf "%s " "$pattern"
+ grep "($pattern):" -r --include='*.py' --exclude-dir=".venv" --exclude-dir="tests" . | wc -l
+done
diff --git a/api/models/dataset.py b/api/models/dataset.py
index 33d396aeb90..4470d113552 100644
--- a/api/models/dataset.py
+++ b/api/models/dataset.py
@@ -225,7 +225,7 @@ class Dataset(Base):
ExternalKnowledgeApis.id == external_knowledge_binding.external_knowledge_api_id
)
)
- if not external_knowledge_api:
+ if external_knowledge_api is None or external_knowledge_api.settings is None:
return None
return {
"external_knowledge_id": external_knowledge_binding.external_knowledge_id,
@@ -945,18 +945,20 @@ class DatasetQuery(Base):
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=sa.func.current_timestamp())
-class DatasetKeywordTable(Base):
+class DatasetKeywordTable(TypeBase):
__tablename__ = "dataset_keyword_tables"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="dataset_keyword_table_pkey"),
sa.Index("dataset_keyword_table_dataset_id_idx", "dataset_id"),
)
- id = mapped_column(StringUUID, primary_key=True, server_default=sa.text("uuid_generate_v4()"))
- dataset_id = mapped_column(StringUUID, nullable=False, unique=True)
- keyword_table = mapped_column(sa.Text, nullable=False)
- data_source_type = mapped_column(
- String(255), nullable=False, server_default=sa.text("'database'::character varying")
+ id: Mapped[str] = mapped_column(
+ StringUUID, primary_key=True, server_default=sa.text("uuid_generate_v4()"), init=False
+ )
+ dataset_id: Mapped[str] = mapped_column(StringUUID, nullable=False, unique=True)
+ keyword_table: Mapped[str] = mapped_column(sa.Text, nullable=False)
+ data_source_type: Mapped[str] = mapped_column(
+ String(255), nullable=False, server_default=sa.text("'database'::character varying"), default="database"
)
@property
@@ -1054,19 +1056,23 @@ class TidbAuthBinding(Base):
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
-class Whitelist(Base):
+class Whitelist(TypeBase):
__tablename__ = "whitelists"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="whitelists_pkey"),
sa.Index("whitelists_tenant_idx", "tenant_id"),
)
- id = mapped_column(StringUUID, primary_key=True, server_default=sa.text("uuid_generate_v4()"))
- tenant_id = mapped_column(StringUUID, nullable=True)
+ id: Mapped[str] = mapped_column(
+ StringUUID, primary_key=True, server_default=sa.text("uuid_generate_v4()"), init=False
+ )
+ tenant_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True)
category: Mapped[str] = mapped_column(String(255), nullable=False)
- created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
+ created_at: Mapped[datetime] = mapped_column(
+ DateTime, nullable=False, server_default=func.current_timestamp(), init=False
+ )
-class DatasetPermission(Base):
+class DatasetPermission(TypeBase):
__tablename__ = "dataset_permissions"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="dataset_permission_pkey"),
@@ -1075,15 +1081,21 @@ class DatasetPermission(Base):
sa.Index("idx_dataset_permissions_tenant_id", "tenant_id"),
)
- id = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), primary_key=True)
- dataset_id = mapped_column(StringUUID, nullable=False)
- account_id = mapped_column(StringUUID, nullable=False)
- tenant_id = mapped_column(StringUUID, nullable=False)
- has_permission: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true"))
- created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
+ id: Mapped[str] = mapped_column(
+ StringUUID, server_default=sa.text("uuid_generate_v4()"), primary_key=True, init=False
+ )
+ dataset_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
+ account_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
+ tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
+ has_permission: Mapped[bool] = mapped_column(
+ sa.Boolean, nullable=False, server_default=sa.text("true"), default=True
+ )
+ created_at: Mapped[datetime] = mapped_column(
+ DateTime, nullable=False, server_default=func.current_timestamp(), init=False
+ )
-class ExternalKnowledgeApis(Base):
+class ExternalKnowledgeApis(TypeBase):
__tablename__ = "external_knowledge_apis"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="external_knowledge_apis_pkey"),
@@ -1091,16 +1103,20 @@ class ExternalKnowledgeApis(Base):
sa.Index("external_knowledge_apis_name_idx", "name"),
)
- id = mapped_column(StringUUID, nullable=False, server_default=sa.text("uuid_generate_v4()"))
+ id: Mapped[str] = mapped_column(
+ StringUUID, nullable=False, server_default=sa.text("uuid_generate_v4()"), init=False
+ )
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[str] = mapped_column(String(255), nullable=False)
- tenant_id = mapped_column(StringUUID, nullable=False)
- settings = mapped_column(sa.Text, nullable=True)
- created_by = mapped_column(StringUUID, nullable=False)
- created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
- updated_by = mapped_column(StringUUID, nullable=True)
+ tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
+ settings: Mapped[str | None] = mapped_column(sa.Text, nullable=True)
+ created_by: Mapped[str] = mapped_column(StringUUID, nullable=False)
+ created_at: Mapped[datetime] = mapped_column(
+ DateTime, nullable=False, server_default=func.current_timestamp(), init=False
+ )
+ updated_by: Mapped[str | None] = mapped_column(StringUUID, nullable=True)
updated_at: Mapped[datetime] = mapped_column(
- DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp()
+ DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp(), init=False
)
def to_dict(self) -> dict[str, Any]:
@@ -1178,7 +1194,7 @@ class DatasetAutoDisableLog(Base):
)
-class RateLimitLog(Base):
+class RateLimitLog(TypeBase):
__tablename__ = "rate_limit_logs"
__table_args__ = (
sa.PrimaryKeyConstraint("id", name="rate_limit_log_pkey"),
@@ -1186,12 +1202,12 @@ class RateLimitLog(Base):
sa.Index("rate_limit_log_operation_idx", "operation"),
)
- id = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
- tenant_id = mapped_column(StringUUID, nullable=False)
+ id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"), init=False)
+ tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
subscription_plan: Mapped[str] = mapped_column(String(255), nullable=False)
operation: Mapped[str] = mapped_column(String(255), nullable=False)
created_at: Mapped[datetime] = mapped_column(
- DateTime, nullable=False, server_default=sa.text("CURRENT_TIMESTAMP(0)")
+ DateTime, nullable=False, server_default=sa.text("CURRENT_TIMESTAMP(0)"), init=False
)
diff --git a/api/models/trigger.py b/api/models/trigger.py
index c2b66ace46c..b537f0cf3f5 100644
--- a/api/models/trigger.py
+++ b/api/models/trigger.py
@@ -14,7 +14,7 @@ from core.trigger.entities.api_entities import TriggerProviderSubscriptionApiEnt
from core.trigger.entities.entities import Subscription
from core.trigger.utils.endpoint import generate_plugin_trigger_endpoint_url, generate_webhook_trigger_endpoint
from libs.datetime_utils import naive_utc_now
-from models.base import Base
+from models.base import Base, TypeBase
from models.engine import db
from models.enums import AppTriggerStatus, AppTriggerType, CreatorUserRole, WorkflowTriggerStatus
from models.model import Account
@@ -399,7 +399,7 @@ class AppTrigger(Base):
)
-class WorkflowSchedulePlan(Base):
+class WorkflowSchedulePlan(TypeBase):
"""
Workflow Schedule Configuration
@@ -425,7 +425,7 @@ class WorkflowSchedulePlan(Base):
sa.Index("workflow_schedule_plan_next_idx", "next_run_at"),
)
- id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuidv7()"))
+ id: Mapped[str] = mapped_column(StringUUID, primary_key=True, server_default=sa.text("uuidv7()"), init=False)
app_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
node_id: Mapped[str] = mapped_column(String(64), nullable=False)
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
@@ -436,9 +436,11 @@ class WorkflowSchedulePlan(Base):
# Schedule control
next_run_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
- created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
+ created_at: Mapped[datetime] = mapped_column(
+ DateTime, nullable=False, server_default=func.current_timestamp(), init=False
+ )
updated_at: Mapped[datetime] = mapped_column(
- DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp()
+ DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp(), init=False
)
def to_dict(self) -> dict[str, Any]:
diff --git a/api/services/external_knowledge_service.py b/api/services/external_knowledge_service.py
index 5cd3b471f97..0c2a80d0675 100644
--- a/api/services/external_knowledge_service.py
+++ b/api/services/external_knowledge_service.py
@@ -62,7 +62,7 @@ class ExternalDatasetService:
tenant_id=tenant_id,
created_by=user_id,
updated_by=user_id,
- name=args.get("name"),
+ name=str(args.get("name")),
description=args.get("description", ""),
settings=json.dumps(args.get("settings"), ensure_ascii=False),
)
@@ -163,7 +163,7 @@ class ExternalDatasetService:
external_knowledge_api = (
db.session.query(ExternalKnowledgeApis).filter_by(id=external_knowledge_api_id, tenant_id=tenant_id).first()
)
- if external_knowledge_api is None:
+ if external_knowledge_api is None or external_knowledge_api.settings is None:
raise ValueError("api template not found")
settings = json.loads(external_knowledge_api.settings)
for setting in settings:
@@ -290,7 +290,7 @@ class ExternalDatasetService:
.filter_by(id=external_knowledge_binding.external_knowledge_api_id)
.first()
)
- if not external_knowledge_api:
+ if external_knowledge_api is None or external_knowledge_api.settings is None:
raise ValueError("external api template not found")
settings = json.loads(external_knowledge_api.settings)