eigent/server/app/model/chat/chat_history.py
Tong Chen 97d7554438
Some checks failed
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Pre-commit / pre-commit (push) Has been cancelled
Test / Run Web + Local Brain Smoke (push) Has been cancelled
Test / Run Frontend Guardrails (push) Has been cancelled
Test / Run Python Tests (push) Has been cancelled
release: Eigent 1.0.0 (#1695)
Co-authored-by: Douglas <douglas.ym.lai@gmail.com>
Co-authored-by: Douglas Lai <115660088+Douglasymlai@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Tao Sun <168447269+fengju0213@users.noreply.github.com>
Co-authored-by: Weijie Bai <happy.regina.bai@gmail.com>
2026-06-17 00:29:35 +08:00

148 lines
5.3 KiB
Python

# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
from datetime import datetime
from enum import IntEnum
from typing import Literal
from pydantic import BaseModel, model_validator
from sqlalchemy import Float, Integer
from sqlalchemy_utils import ChoiceType
from sqlmodel import JSON, Column, Field, SmallInteger, String
from app.model.abstract.model import AbstractModel, DefaultTimes
from app.shared.types.space_types import SkipReason
class ChatStatus(IntEnum):
ongoing = 1
done = 2
class ChatHistory(AbstractModel, DefaultTimes, table=True):
"""
Chat history model with timestamp tracking.
Inherits from DefaultTimes which provides:
- created_at: timestamp when record is created (auto-populated)
- updated_at: timestamp when record is last modified (auto-updated)
- deleted_at: timestamp for soft deletion (nullable)
For legacy records without timestamps, sorting falls back to id ordering.
"""
id: int = Field(default=None, primary_key=True)
user_id: int = Field(index=True)
task_id: str = Field(index=True, unique=True)
project_id: str = Field(index=True, unique=False, nullable=True)
space_id: str | None = Field(default=None, index=True)
run_id: str | None = Field(default=None, index=True)
question: str
language: str
model_platform: str
model_type: str
api_key: str
api_url: str = Field(sa_column=Column(String(500)))
max_retries: int = Field(default=3)
file_save_path: str | None = None
installed_mcp: str = Field(sa_type=JSON, default={})
project_name: str = Field(default="", sa_column=Column(String(128)))
summary: str = Field(default="", sa_column=Column(String(1024)))
tokens: int = Field(default=0, sa_column=(Column(Integer, server_default="0")))
spend: float = Field(default=0, sa_column=(Column(Float, server_default="0")))
status: int = Field(default=1, sa_column=Column(ChoiceType(ChatStatus, SmallInteger())))
skip_reason: SkipReason | None = Field(default=None, sa_column=Column(JSON))
class ChatHistoryIn(BaseModel):
task_id: str
project_id: str | None = None
space_id: str | None = None
run_id: str | None = None
user_id: int | None = None
question: str
language: str
model_platform: str
model_type: str
api_key: str | None = ""
api_url: str | None = None
max_retries: int = 3
file_save_path: str | None = None
installed_mcp: str | None = None
project_name: str | None = None
summary: str | None = None
tokens: int = 0
spend: float = 0
status: int = ChatStatus.ongoing.value
skip_reason: SkipReason | None = None
workdir_mode: str | None = None
# Project execution mode reported by the client when starting the chat.
# Persisted on the Project row so reload reflects the user's last choice
# (single-agent vs workforce) — without this, Project.mode stays NULL and
# reload defaults the UI back to single-agent.
# Constrained to a Literal so a bad value gets a 422 from pydantic instead
# of bubbling up as "Space not found" through ensure_project's ValueError
# → 404 mapping in history_controller.
mode: Literal["single-agent", "workforce"] | None = None
class ChatHistoryOut(BaseModel):
id: int
task_id: str
project_id: str | None = None
space_id: str | None = None
run_id: str | None = None
question: str
language: str
model_platform: str
model_type: str
api_key: str | None = None
api_url: str | None = None
max_retries: int
file_save_path: str | None = None
installed_mcp: str | None = None
project_name: str | None = None
summary: str | None = None
tokens: int
status: int
skip_reason: SkipReason | None = None
created_at: datetime | None = None
updated_at: datetime | None = None
@model_validator(mode="after")
def fill_project_id_from_task_id(self):
"""Fill project_id from task_id when project_id is None"""
if self.project_id is None:
self.project_id = self.task_id
if self.run_id is None:
self.run_id = self.task_id
return self
@model_validator(mode="after")
def handle_legacy_timestamps(self):
"""Handle legacy records that might not have timestamp fields"""
# For old records without timestamps, we rely on database-level defaults
# The sorting in the controller will handle ordering appropriately
return self
class ChatHistoryUpdate(BaseModel):
project_name: str | None = None
summary: str | None = None
tokens: int | None = None
status: int | None = None
project_id: str | None = None
space_id: str | None = None
run_id: str | None = None
skip_reason: SkipReason | None = None