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(api): add SQLAlchemy 2.x Mapped type hints to Message model (#27709)

-LAN-4461df1bd900
diff --git a/api/models/model.py b/api/models/model.py
index 8a8574e2fed..818021eb42a 100644
--- a/api/models/model.py
+++ b/api/models/model.py
@@ -3,6 +3,7 @@ import re
import uuid
from collections.abc import Mapping
from datetime import datetime
+from decimal import Decimal
from enum import StrEnum, auto
from typing import TYPE_CHECKING, Any, Literal, Optional, cast
@@ -914,34 +915,40 @@ class Message(Base):
)
id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
- app_id = mapped_column(StringUUID, nullable=False)
- model_provider = mapped_column(String(255), nullable=True)
- model_id = mapped_column(String(255), nullable=True)
- override_model_configs = mapped_column(sa.Text)
- conversation_id = mapped_column(StringUUID, sa.ForeignKey("conversations.id"), nullable=False)
+ app_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
+ model_provider: Mapped[str | None] = mapped_column(String(255), nullable=True)
+ model_id: Mapped[str | None] = mapped_column(String(255), nullable=True)
+ override_model_configs: Mapped[str | None] = mapped_column(sa.Text)
+ conversation_id: Mapped[str] = mapped_column(StringUUID, sa.ForeignKey("conversations.id"), nullable=False)
_inputs: Mapped[dict[str, Any]] = mapped_column("inputs", sa.JSON)
query: Mapped[str] = mapped_column(sa.Text, nullable=False)
- message = mapped_column(sa.JSON, nullable=False)
+ message: Mapped[dict[str, Any]] = mapped_column(sa.JSON, nullable=False)
message_tokens: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0"))
- message_unit_price = mapped_column(sa.Numeric(10, 4), nullable=False)
- message_price_unit = mapped_column(sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001"))
+ message_unit_price: Mapped[Decimal] = mapped_column(sa.Numeric(10, 4), nullable=False)
+ message_price_unit: Mapped[Decimal] = mapped_column(
+ sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001")
+ )
answer: Mapped[str] = mapped_column(sa.Text, nullable=False)
answer_tokens: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0"))
- answer_unit_price = mapped_column(sa.Numeric(10, 4), nullable=False)
- answer_price_unit = mapped_column(sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001"))
- parent_message_id = mapped_column(StringUUID, nullable=True)
- provider_response_latency = mapped_column(sa.Float, nullable=False, server_default=sa.text("0"))
- total_price = mapped_column(sa.Numeric(10, 7))
+ answer_unit_price: Mapped[Decimal] = mapped_column(sa.Numeric(10, 4), nullable=False)
+ answer_price_unit: Mapped[Decimal] = mapped_column(
+ sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001")
+ )
+ parent_message_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True)
+ provider_response_latency: Mapped[float] = mapped_column(sa.Float, nullable=False, server_default=sa.text("0"))
+ total_price: Mapped[Decimal | None] = mapped_column(sa.Numeric(10, 7))
currency: Mapped[str] = mapped_column(String(255), nullable=False)
- status = mapped_column(String(255), nullable=False, server_default=sa.text("'normal'::character varying"))
- error = mapped_column(sa.Text)
- message_metadata = mapped_column(sa.Text)
+ status: Mapped[str] = mapped_column(
+ String(255), nullable=False, server_default=sa.text("'normal'::character varying")
+ )
+ error: Mapped[str | None] = mapped_column(sa.Text)
+ message_metadata: Mapped[str | None] = mapped_column(sa.Text)
invoke_from: Mapped[str | None] = mapped_column(String(255), nullable=True)
from_source: Mapped[str] = mapped_column(String(255), nullable=False)
from_end_user_id: Mapped[str | None] = mapped_column(StringUUID)
from_account_id: Mapped[str | None] = mapped_column(StringUUID)
created_at: Mapped[datetime] = mapped_column(sa.DateTime, server_default=func.current_timestamp())
- updated_at = 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())
agent_based: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"))
workflow_run_id: Mapped[str | None] = mapped_column(StringUUID)
app_mode: Mapped[str | None] = mapped_column(String(255), nullable=True)