Initial commit: clean project structure

- Backend: FastAPI + SQLAlchemy + Celery (Python 3.11+)
- Frontend: Vue 3 + TypeScript + Pinia + Tailwind
- Admin Frontend: separate Vue 3 app for management
- Docker Compose: 9 services orchestration
- Specs: design prototypes, memory system PRD, product roadmap

Cleanup performed:
- Removed temporary debug scripts from backend root
- Removed deprecated admin_app.py (embedded UI)
- Removed duplicate docs from admin-frontend
- Updated .gitignore for Vite cache and egg-info
This commit is contained in:
zhangtuo
2026-01-20 18:20:03 +08:00
commit e9d7f8832a
241 changed files with 33070 additions and 0 deletions

View File

View File

@@ -0,0 +1,119 @@
from datetime import datetime
from decimal import Decimal
from uuid import uuid4
from sqlalchemy import JSON, Boolean, DateTime, ForeignKey, Integer, Numeric, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.db.models import Base
def _uuid() -> str:
return str(uuid4())
class Provider(Base):
"""Model provider registry."""
__tablename__ = "providers"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
name: Mapped[str] = mapped_column(String(100), nullable=False)
type: Mapped[str] = mapped_column(String(50), nullable=False) # text/image/tts/storybook
adapter: Mapped[str] = mapped_column(String(100), nullable=False)
model: Mapped[str] = mapped_column(String(200), nullable=True)
api_base: Mapped[str] = mapped_column(String(300), nullable=True)
api_key: Mapped[str] = mapped_column(String(500), nullable=True) # 可选,优先于 config_ref
timeout_ms: Mapped[int] = mapped_column(Integer, default=60000)
max_retries: Mapped[int] = mapped_column(Integer, default=1)
weight: Mapped[int] = mapped_column(Integer, default=1)
priority: Mapped[int] = mapped_column(Integer, default=0)
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
config_json: Mapped[dict | None] = mapped_column(JSON, nullable=True) # 存储额外配置(speed, vol, etc)
config_ref: Mapped[str] = mapped_column(String(100), nullable=True) # 环境变量 key 名称(回退)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=datetime.utcnow, onupdate=datetime.utcnow
)
updated_by: Mapped[str] = mapped_column(String(100), nullable=True)
class ProviderMetrics(Base):
"""供应商调用指标记录。"""
__tablename__ = "provider_metrics"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
provider_id: Mapped[str] = mapped_column(
String(36), ForeignKey("providers.id", ondelete="CASCADE"), nullable=False, index=True
)
timestamp: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=datetime.utcnow, index=True
)
success: Mapped[bool] = mapped_column(Boolean, nullable=False)
latency_ms: Mapped[int] = mapped_column(Integer, nullable=True)
cost_usd: Mapped[Decimal] = mapped_column(Numeric(10, 6), nullable=True)
error_message: Mapped[str] = mapped_column(Text, nullable=True)
request_id: Mapped[str] = mapped_column(String(100), nullable=True)
class ProviderHealth(Base):
"""供应商健康状态。"""
__tablename__ = "provider_health"
provider_id: Mapped[str] = mapped_column(
String(36), ForeignKey("providers.id", ondelete="CASCADE"), primary_key=True
)
is_healthy: Mapped[bool] = mapped_column(Boolean, default=True)
last_check: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=True)
consecutive_failures: Mapped[int] = mapped_column(Integer, default=0)
last_error: Mapped[str] = mapped_column(Text, nullable=True)
class ProviderSecret(Base):
"""供应商密钥加密存储。"""
__tablename__ = "provider_secrets"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
encrypted_value: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=datetime.utcnow, onupdate=datetime.utcnow
)
class CostRecord(Base):
"""成本记录表 - 记录每次 API 调用的成本。"""
__tablename__ = "cost_records"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
user_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
provider_id: Mapped[str] = mapped_column(String(36), nullable=True) # 可能是环境变量配置
provider_name: Mapped[str] = mapped_column(String(100), nullable=False)
capability: Mapped[str] = mapped_column(String(50), nullable=False) # text/image/tts/storybook
estimated_cost: Mapped[Decimal] = mapped_column(Numeric(10, 6), nullable=False)
timestamp: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=datetime.utcnow, index=True
)
class UserBudget(Base):
"""用户预算配置。"""
__tablename__ = "user_budgets"
user_id: Mapped[str] = mapped_column(String(36), primary_key=True)
daily_limit_usd: Mapped[Decimal] = mapped_column(Numeric(10, 4), default=Decimal("1.0"))
monthly_limit_usd: Mapped[Decimal] = mapped_column(Numeric(10, 4), default=Decimal("10.0"))
alert_threshold: Mapped[Decimal] = mapped_column(
Numeric(3, 2), default=Decimal("0.8")
) # 80% 时告警
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=datetime.utcnow, onupdate=datetime.utcnow
)

View File

@@ -0,0 +1,50 @@
import threading
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.core.config import settings
_engine = None
_session_factory: async_sessionmaker[AsyncSession] | None = None
_lock = threading.Lock()
def _get_engine():
global _engine
if _engine is None:
with _lock:
if _engine is None:
_engine = create_async_engine(
settings.database_url,
echo=settings.debug,
pool_pre_ping=True,
pool_recycle=300,
)
return _engine
def _get_session_factory():
global _session_factory
if _session_factory is None:
with _lock:
if _session_factory is None:
_session_factory = async_sessionmaker(
_get_engine(), class_=AsyncSession, expire_on_commit=False
)
return _session_factory
async def init_db():
"""Create tables if they do not exist."""
from app.db.models import Base # main models
engine = _get_engine()
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async def get_db():
"""Yield a DB session with proper cleanup."""
session_factory = _get_session_factory()
async with session_factory() as session:
yield session

232
backend/app/db/models.py Normal file
View File

@@ -0,0 +1,232 @@
from datetime import date, datetime, time
from uuid import uuid4
from sqlalchemy import (
JSON,
Boolean,
Date,
DateTime,
Float,
ForeignKey,
Integer,
String,
Text,
Time,
UniqueConstraint,
func,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
"""Declarative base."""
class User(Base):
"""User entity."""
__tablename__ = "users"
id: Mapped[str] = mapped_column(String(255), primary_key=True) # OAuth provider user ID
name: Mapped[str] = mapped_column(String(255), nullable=False)
avatar_url: Mapped[str | None] = mapped_column(String(500))
provider: Mapped[str] = mapped_column(String(50), nullable=False) # github / google
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
stories: Mapped[list["Story"]] = relationship(
"Story", back_populates="user", cascade="all, delete-orphan"
)
child_profiles: Mapped[list["ChildProfile"]] = relationship(
"ChildProfile", back_populates="user", cascade="all, delete-orphan"
)
class Story(Base):
"""Story entity."""
__tablename__ = "stories"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
user_id: Mapped[str] = mapped_column(
String(255), ForeignKey("users.id", ondelete="CASCADE"), nullable=False
)
child_profile_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("child_profiles.id", ondelete="SET NULL"), nullable=True
)
universe_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("story_universes.id", ondelete="SET NULL"), nullable=True
)
title: Mapped[str] = mapped_column(String(255), nullable=False)
story_text: Mapped[str] = mapped_column(Text, nullable=True) # 允许为空(绘本模式下)
pages: Mapped[list[dict] | None] = mapped_column(JSON, default=list) # 绘本分页数据
cover_prompt: Mapped[str | None] = mapped_column(Text)
image_url: Mapped[str | None] = mapped_column(String(500))
mode: Mapped[str] = mapped_column(String(20), nullable=False, default="generated")
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
user: Mapped["User"] = relationship("User", back_populates="stories")
child_profile: Mapped["ChildProfile | None"] = relationship("ChildProfile")
story_universe: Mapped["StoryUniverse | None"] = relationship("StoryUniverse")
def _uuid() -> str:
return str(uuid4())
class ChildProfile(Base):
"""Child profile entity."""
__tablename__ = "child_profiles"
__table_args__ = (UniqueConstraint("user_id", "name", name="uq_child_profile_user_name"),)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
user_id: Mapped[str] = mapped_column(
String(255), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
name: Mapped[str] = mapped_column(String(50), nullable=False)
avatar_url: Mapped[str | None] = mapped_column(String(500))
birth_date: Mapped[date | None] = mapped_column(Date)
gender: Mapped[str | None] = mapped_column(String(10))
interests: Mapped[list[str]] = mapped_column(JSON, default=list)
growth_themes: Mapped[list[str]] = mapped_column(JSON, default=list)
reading_preferences: Mapped[dict] = mapped_column(JSON, default=dict)
stories_count: Mapped[int] = mapped_column(Integer, default=0)
total_reading_time: Mapped[int] = mapped_column(Integer, default=0)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
user: Mapped["User"] = relationship("User", back_populates="child_profiles")
story_universes: Mapped[list["StoryUniverse"]] = relationship(
"StoryUniverse", back_populates="child_profile", cascade="all, delete-orphan"
)
@property
def age(self) -> int | None:
if not self.birth_date:
return None
today = date.today()
return today.year - self.birth_date.year - (
(today.month, today.day) < (self.birth_date.month, self.birth_date.day)
)
class StoryUniverse(Base):
"""Story universe entity."""
__tablename__ = "story_universes"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
child_profile_id: Mapped[str] = mapped_column(
String(36), ForeignKey("child_profiles.id", ondelete="CASCADE"), nullable=False, index=True
)
name: Mapped[str] = mapped_column(String(100), nullable=False)
protagonist: Mapped[dict] = mapped_column(JSON, nullable=False)
recurring_characters: Mapped[list] = mapped_column(JSON, default=list)
world_settings: Mapped[dict] = mapped_column(JSON, default=dict)
achievements: Mapped[list] = mapped_column(JSON, default=list)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
child_profile: Mapped["ChildProfile"] = relationship("ChildProfile", back_populates="story_universes")
class ReadingEvent(Base):
"""Reading event entity."""
__tablename__ = "reading_events"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
child_profile_id: Mapped[str] = mapped_column(
String(36), ForeignKey("child_profiles.id", ondelete="CASCADE"), nullable=False, index=True
)
story_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("stories.id", ondelete="SET NULL"), nullable=True, index=True
)
event_type: Mapped[str] = mapped_column(String(20), nullable=False)
reading_time: Mapped[int] = mapped_column(Integer, default=0)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), index=True
)
class PushConfig(Base):
"""Push configuration entity."""
__tablename__ = "push_configs"
__table_args__ = (
UniqueConstraint("child_profile_id", name="uq_push_config_child"),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
user_id: Mapped[str] = mapped_column(
String(255), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
child_profile_id: Mapped[str] = mapped_column(
String(36), ForeignKey("child_profiles.id", ondelete="CASCADE"), nullable=False, index=True
)
push_time: Mapped[time | None] = mapped_column(Time)
push_days: Mapped[list[int]] = mapped_column(JSON, default=list)
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
class PushEvent(Base):
"""Push event entity."""
__tablename__ = "push_events"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
user_id: Mapped[str] = mapped_column(
String(255), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
child_profile_id: Mapped[str] = mapped_column(
String(36), ForeignKey("child_profiles.id", ondelete="CASCADE"), nullable=False, index=True
)
trigger_type: Mapped[str] = mapped_column(String(20), nullable=False)
status: Mapped[str] = mapped_column(String(20), nullable=False)
reason: Mapped[str | None] = mapped_column(Text)
sent_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, index=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
class MemoryItem(Base):
"""Memory item entity with time decay metadata."""
__tablename__ = "memory_items"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
child_profile_id: Mapped[str] = mapped_column(
String(36), ForeignKey("child_profiles.id", ondelete="CASCADE"), nullable=False, index=True
)
universe_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("story_universes.id", ondelete="SET NULL"), nullable=True, index=True
)
type: Mapped[str] = mapped_column(String(50), nullable=False)
value: Mapped[dict] = mapped_column(JSON, nullable=False)
base_weight: Mapped[float] = mapped_column(Float, default=1.0)
last_used_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
ttl_days: Mapped[int | None] = mapped_column(Integer)