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.

refactor(models): Add mapped type hints to MessageAnnotation (#27751)

-LAN-a99ac3fe0df4
diff --git a/api/commands.py b/api/commands.py
index 44f7b428258..7ebf5b4874e 100644
--- a/api/commands.py
+++ b/api/commands.py
@@ -235,7 +235,7 @@ def migrate_annotation_vector_database():
if annotations:
for annotation in annotations:
document = Document(
- page_content=annotation.question,
+ page_content=annotation.question_text,
metadata={"annotation_id": annotation.id, "app_id": app.id, "doc_id": annotation.id},
)
documents.append(document)
diff --git a/api/core/app/features/annotation_reply/annotation_reply.py b/api/core/app/features/annotation_reply/annotation_reply.py
index 79fbafe39e0..3f9f3da9b2f 100644
--- a/api/core/app/features/annotation_reply/annotation_reply.py
+++ b/api/core/app/features/annotation_reply/annotation_reply.py
@@ -75,7 +75,7 @@ class AnnotationReplyFeature:
AppAnnotationService.add_annotation_history(
annotation.id,
app_record.id,
- annotation.question,
+ annotation.question_text,
annotation.content,
query,
user_id,
diff --git a/api/models/model.py b/api/models/model.py
index 88cb945b3f0..6cfcc2859d7 100644
--- a/api/models/model.py
+++ b/api/models/model.py
@@ -1419,15 +1419,20 @@ class MessageAnnotation(Base):
app_id: Mapped[str] = mapped_column(StringUUID)
conversation_id: Mapped[str | None] = mapped_column(StringUUID, sa.ForeignKey("conversations.id"))
message_id: Mapped[str | None] = mapped_column(StringUUID)
- question = mapped_column(LongText, nullable=True)
- content = mapped_column(LongText, nullable=False)
+ question: Mapped[str | None] = mapped_column(LongText, nullable=True)
+ content: Mapped[str] = mapped_column(LongText, nullable=False)
hit_count: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0"))
- account_id = mapped_column(StringUUID, nullable=False)
- created_at = mapped_column(sa.DateTime, nullable=False, server_default=func.current_timestamp())
- updated_at = mapped_column(
+ account_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
+ created_at: Mapped[datetime] = mapped_column(sa.DateTime, nullable=False, server_default=func.current_timestamp())
+ updated_at: Mapped[datetime] = mapped_column(
sa.DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp()
)
+ @property
+ def question_text(self) -> str:
+ """Return a non-null question string, falling back to the answer content."""
+ return self.question or self.content
+
@property
def account(self):
account = db.session.query(Account).where(Account.id == self.account_id).first()
diff --git a/api/services/annotation_service.py b/api/services/annotation_service.py
index d03cbddceb1..7f44fe05a66 100644
--- a/api/services/annotation_service.py
+++ b/api/services/annotation_service.py
@@ -77,7 +77,7 @@ class AppAnnotationService:
if annotation_setting:
add_annotation_to_index_task.delay(
annotation.id,
- annotation.question,
+ question,
current_tenant_id,
app_id,
annotation_setting.collection_binding_id,
@@ -253,7 +253,7 @@ class AppAnnotationService:
if app_annotation_setting:
update_annotation_to_index_task.delay(
annotation.id,
- annotation.question,
+ annotation.question_text,
current_tenant_id,
app_id,
app_annotation_setting.collection_binding_id,
diff --git a/api/tasks/annotation/enable_annotation_reply_task.py b/api/tasks/annotation/enable_annotation_reply_task.py
index cdc07c77a86..be1de3cdd2b 100644
--- a/api/tasks/annotation/enable_annotation_reply_task.py
+++ b/api/tasks/annotation/enable_annotation_reply_task.py
@@ -98,7 +98,7 @@ def enable_annotation_reply_task(
if annotations:
for annotation in annotations:
document = Document(
- page_content=annotation.question,
+ page_content=annotation.question_text,
metadata={"annotation_id": annotation.id, "app_id": app_id, "doc_id": annotation.id},
)
documents.append(document)