门户估价对话、会员与智能体账号,以及生产 Docker 部署一直停在工作区,远端主分支还没有这些改动。 Co-authored-by: Cursor <cursoragent@cursor.com>
361 lines
10 KiB
Python
361 lines
10 KiB
Python
from sqlalchemy import func, select
|
|
from sqlalchemy.orm import Session
|
|
|
|
import json
|
|
|
|
from app.core.config import get_settings
|
|
from app.models.member import SiteMember
|
|
from app.services.dify_chat import cited_documents, split_stored_reasoning
|
|
from app.services.valuation_context import DRAFT_FORMATS, ensure_answer_note
|
|
from app.models.rbac import AdminUser
|
|
from app.models.valuation import (
|
|
WELCOME,
|
|
ValuationFile,
|
|
ValuationMessage,
|
|
ValuationSession,
|
|
ValuationSuggestion,
|
|
ValuationTemplate,
|
|
_now,
|
|
attachment_names,
|
|
stored_knowledge_names,
|
|
ensure_default_suggestions,
|
|
ensure_default_template,
|
|
)
|
|
from app.services.storage import remove_stored_object
|
|
|
|
|
|
def member_can_manage(db: Session, member: SiteMember) -> bool:
|
|
email = (member.email or "").strip().lower()
|
|
if not email:
|
|
return False
|
|
extra = {
|
|
item.strip().lower()
|
|
for item in (get_settings().valuation_admin_emails or "").split(",")
|
|
if item.strip()
|
|
}
|
|
if email in extra:
|
|
return True
|
|
matched = db.scalar(
|
|
select(AdminUser.id).where(
|
|
func.lower(AdminUser.username) == email,
|
|
AdminUser.status == "active",
|
|
)
|
|
)
|
|
return matched is not None
|
|
|
|
|
|
def suggestion_payload(row: ValuationSuggestion) -> dict:
|
|
return {
|
|
"id": row.id,
|
|
"text": row.text,
|
|
"enabled": bool(row.enabled),
|
|
"sortOrder": row.sort_order,
|
|
}
|
|
|
|
|
|
def list_suggestions(db: Session, *, enabled_only: bool) -> list[dict]:
|
|
ensure_default_suggestions(db)
|
|
stmt = select(ValuationSuggestion).order_by(ValuationSuggestion.sort_order, ValuationSuggestion.id)
|
|
if enabled_only:
|
|
stmt = stmt.where(ValuationSuggestion.enabled.is_(True))
|
|
return [suggestion_payload(row) for row in db.scalars(stmt)]
|
|
|
|
|
|
def add_suggestion(db: Session, text: str) -> dict:
|
|
clean = text.strip()
|
|
current = db.scalar(select(func.max(ValuationSuggestion.sort_order))) or 0
|
|
row = ValuationSuggestion(text=clean, sort_order=int(current) + 1, enabled=True, created_at=_now())
|
|
db.add(row)
|
|
db.commit()
|
|
db.refresh(row)
|
|
return suggestion_payload(row)
|
|
|
|
|
|
def update_suggestion(db: Session, suggestion_id: int, *, text: str | None, enabled: bool | None) -> dict | None:
|
|
row = db.get(ValuationSuggestion, suggestion_id)
|
|
if row is None:
|
|
return None
|
|
if text is not None:
|
|
row.text = text.strip()
|
|
if enabled is not None:
|
|
row.enabled = enabled
|
|
db.commit()
|
|
db.refresh(row)
|
|
return suggestion_payload(row)
|
|
|
|
|
|
def delete_suggestion(db: Session, suggestion_id: int) -> bool:
|
|
row = db.get(ValuationSuggestion, suggestion_id)
|
|
if row is None:
|
|
return False
|
|
db.delete(row)
|
|
db.commit()
|
|
return True
|
|
|
|
|
|
def session_payload(row: ValuationSession) -> dict:
|
|
return {
|
|
"id": row.id,
|
|
"title": row.title,
|
|
"updatedAt": row.updated_at.isoformat(timespec="seconds"),
|
|
}
|
|
|
|
|
|
def message_payload(row: ValuationMessage) -> dict:
|
|
content, thought = split_stored_reasoning(row.content, row.thought or "")
|
|
if row.role == "assistant" and (row.kind or "chat") == "chat" and content.strip() != WELCOME.strip():
|
|
content = ensure_answer_note(content)
|
|
return {
|
|
"id": row.id,
|
|
"role": row.role,
|
|
"content": content,
|
|
"thought": thought,
|
|
"attachments": attachment_names(row.attachments or ""),
|
|
"web": bool(row.web_enabled),
|
|
"kind": row.kind or "chat",
|
|
"draftFormat": message_draft_format(row),
|
|
"draftName": (row.draft_name or "").strip(),
|
|
"sources": attachment_names(getattr(row, "sources", "") or ""),
|
|
"createdAt": row.created_at.isoformat(timespec="seconds"),
|
|
}
|
|
|
|
|
|
def list_sessions(db: Session, owner: str) -> list[dict]:
|
|
rows = db.scalars(
|
|
select(ValuationSession)
|
|
.where(ValuationSession.owner_key == owner)
|
|
.order_by(ValuationSession.updated_at.desc(), ValuationSession.id.desc())
|
|
)
|
|
return [session_payload(row) for row in rows]
|
|
|
|
|
|
def get_owned_session(db: Session, owner: str, session_id: int) -> ValuationSession | None:
|
|
row = db.get(ValuationSession, session_id)
|
|
if row is None or row.owner_key != owner:
|
|
return None
|
|
return row
|
|
|
|
|
|
def create_session(db: Session, owner: str) -> dict:
|
|
now = _now()
|
|
row = ValuationSession(member_id=0, owner_key=owner, title="新会话", created_at=now, updated_at=now)
|
|
db.add(row)
|
|
db.flush()
|
|
db.add(
|
|
ValuationMessage(
|
|
session_id=row.id,
|
|
role="assistant",
|
|
content=WELCOME,
|
|
thought="",
|
|
created_at=now,
|
|
)
|
|
)
|
|
db.commit()
|
|
db.refresh(row)
|
|
return session_payload(row)
|
|
|
|
|
|
def rename_session(db: Session, owner: str, session_id: int, title: str) -> dict | None:
|
|
row = get_owned_session(db, owner, session_id)
|
|
if row is None:
|
|
return None
|
|
row.title = title.strip()[:40]
|
|
row.updated_at = _now()
|
|
db.commit()
|
|
db.refresh(row)
|
|
return session_payload(row)
|
|
|
|
|
|
def delete_session(db: Session, owner: str, session_id: int) -> bool:
|
|
row = get_owned_session(db, owner, session_id)
|
|
if row is None:
|
|
return False
|
|
files = list(db.scalars(select(ValuationFile).where(ValuationFile.session_id == row.id)))
|
|
for item in files:
|
|
try:
|
|
remove_stored_object("private", item.object_key)
|
|
except Exception:
|
|
pass
|
|
db.delete(item)
|
|
db.query(ValuationMessage).filter(ValuationMessage.session_id == row.id).delete()
|
|
db.delete(row)
|
|
db.commit()
|
|
return True
|
|
|
|
|
|
def list_messages(db: Session, owner: str, session_id: int) -> list[dict] | None:
|
|
row = get_owned_session(db, owner, session_id)
|
|
if row is None:
|
|
return None
|
|
messages = db.scalars(
|
|
select(ValuationMessage)
|
|
.where(ValuationMessage.session_id == row.id)
|
|
.order_by(ValuationMessage.created_at, ValuationMessage.id)
|
|
)
|
|
payloads = []
|
|
attached: list[str] = []
|
|
for item in messages:
|
|
payload = message_payload(item)
|
|
if payload["role"] == "user":
|
|
attached = payload["attachments"]
|
|
elif payload["role"] == "assistant":
|
|
payload["sources"] = cited_documents(attached, stored_knowledge_names(item.sources or ""))
|
|
attached = []
|
|
payloads.append(payload)
|
|
return payloads
|
|
|
|
|
|
def add_user_message(
|
|
db: Session,
|
|
session: ValuationSession,
|
|
content: str,
|
|
*,
|
|
attachments: list[str] | None = None,
|
|
web: bool = False,
|
|
) -> ValuationMessage:
|
|
import json
|
|
|
|
now = _now()
|
|
title_source = content.strip() or (attachments[0] if attachments else "")
|
|
if session.title == "新会话" and title_source:
|
|
session.title = title_source[:22]
|
|
session.updated_at = now
|
|
row = ValuationMessage(
|
|
session_id=session.id,
|
|
role="user",
|
|
content=content,
|
|
thought="",
|
|
attachments=json.dumps(attachments or [], ensure_ascii=False),
|
|
web_enabled=web,
|
|
created_at=now,
|
|
)
|
|
db.add(row)
|
|
db.commit()
|
|
db.refresh(session)
|
|
db.refresh(row)
|
|
return row
|
|
|
|
|
|
def owned_files(db: Session, owner: str, file_ids: list[int]) -> list[ValuationFile] | None:
|
|
if not file_ids:
|
|
return []
|
|
rows = list(db.scalars(select(ValuationFile).where(ValuationFile.id.in_(file_ids))))
|
|
found = {row.id: row for row in rows}
|
|
ordered = []
|
|
for file_id in file_ids:
|
|
row = found.get(file_id)
|
|
if row is None or row.owner_key != owner:
|
|
return None
|
|
ordered.append(row)
|
|
return ordered
|
|
|
|
|
|
def bind_files(db: Session, files: list[ValuationFile], session_id: int) -> None:
|
|
for row in files:
|
|
row.session_id = session_id
|
|
db.commit()
|
|
|
|
|
|
def template_format(filename: str) -> str:
|
|
extension = filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
|
|
return "pptx" if extension == "pptx" else "docx"
|
|
|
|
|
|
def message_draft_format(row: ValuationMessage) -> str:
|
|
if (row.kind or "") != "draft":
|
|
return ""
|
|
value = (row.draft_format or "docx").lower()
|
|
return value if value in DRAFT_FORMATS else "docx"
|
|
|
|
|
|
def template_payload(row: ValuationTemplate) -> dict:
|
|
return {
|
|
"id": row.id,
|
|
"name": row.name,
|
|
"filename": row.filename,
|
|
"format": template_format(row.filename),
|
|
"enabled": bool(row.enabled),
|
|
}
|
|
|
|
|
|
def list_templates(db: Session, *, enabled_only: bool) -> list[dict]:
|
|
ensure_default_template(db)
|
|
stmt = select(ValuationTemplate).order_by(ValuationTemplate.id)
|
|
if enabled_only:
|
|
stmt = stmt.where(ValuationTemplate.enabled.is_(True))
|
|
return [template_payload(row) for row in db.scalars(stmt)]
|
|
|
|
|
|
def get_template(db: Session, template_id: int, *, enabled_only: bool) -> ValuationTemplate | None:
|
|
row = db.get(ValuationTemplate, template_id)
|
|
if row is None or (enabled_only and not row.enabled):
|
|
return None
|
|
return row
|
|
|
|
|
|
def add_template(db: Session, *, name: str, filename: str, object_key: str, outline: str) -> dict:
|
|
row = ValuationTemplate(
|
|
name=name.strip()[:80],
|
|
filename=filename[:255],
|
|
object_key=object_key,
|
|
outline=outline,
|
|
enabled=True,
|
|
created_at=_now(),
|
|
)
|
|
db.add(row)
|
|
db.commit()
|
|
db.refresh(row)
|
|
return template_payload(row)
|
|
|
|
|
|
def set_template_enabled(db: Session, template_id: int, enabled: bool) -> dict | None:
|
|
row = db.get(ValuationTemplate, template_id)
|
|
if row is None:
|
|
return None
|
|
row.enabled = enabled
|
|
db.commit()
|
|
db.refresh(row)
|
|
return template_payload(row)
|
|
|
|
|
|
def delete_template(db: Session, template_id: int) -> ValuationTemplate | None:
|
|
row = db.get(ValuationTemplate, template_id)
|
|
if row is None:
|
|
return None
|
|
db.delete(row)
|
|
db.commit()
|
|
return row
|
|
|
|
|
|
def save_assistant_message(
|
|
db: Session,
|
|
session: ValuationSession,
|
|
*,
|
|
content: str,
|
|
thought: str,
|
|
conversation_id: str,
|
|
kind: str = "chat",
|
|
draft_format: str = "",
|
|
draft_name: str = "",
|
|
sources: list[str] | None = None,
|
|
) -> ValuationMessage:
|
|
now = _now()
|
|
if conversation_id:
|
|
session.dify_conversation_id = conversation_id[:64]
|
|
session.updated_at = now
|
|
row = ValuationMessage(
|
|
session_id=session.id,
|
|
role="assistant",
|
|
content=content,
|
|
thought=thought,
|
|
kind=kind,
|
|
draft_format=draft_format if draft_format in DRAFT_FORMATS else "",
|
|
draft_name=draft_name.strip()[:80],
|
|
sources=json.dumps(sources or [], ensure_ascii=False),
|
|
created_at=now,
|
|
)
|
|
db.add(row)
|
|
db.commit()
|
|
db.refresh(row)
|
|
return row
|