feat: Phase 0 — monorepo 骨架 + 全表迁移 + FastAPI/Next 骨架 + CI

- uv(workspace) + pnpm monorepo;docker-compose(pg+api+web)
- SQLAlchemy 16 MVP 表 + Alembic 初版迁移(无漂移,users stub)
- FastAPI 骨架:统一错误信封(带 request_id) + structlog + /jobs/:id + OpenAPI
- Next.js 骨架:纸感主题 token + OpenAPI→TS 客户端代码生成(gen:api)
- CI(ruff/mypy/pytest + pg service + alembic 漂移校验)
- 四份设计规格(PRODUCT/UX/ARCHITECTURE/DEV_PLAN) + CLAUDE.md
This commit is contained in:
Yaojia Wang
2026-06-18 11:38:28 +02:00
commit d3dc620a71
74 changed files with 12960 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
"""Alembic async envCLAUDE.mdcheckpointer.setup 在迁移/CI此处仅领域表"""
from __future__ import annotations
import asyncio
from logging.config import fileConfig
import ww_db.models # noqa: F401 注册所有表到 metadata
from alembic import context
from sqlalchemy import Connection, pool
from sqlalchemy.ext.asyncio import async_engine_from_config
from ww_config import get_settings
from ww_db.base import Base
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# 用 async URLasyncpg运行迁移
config.set_main_option("sqlalchemy.url", get_settings().database_url)
target_metadata = Base.metadata
def do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run_migrations_offline() -> None:
context.configure(
url=config.get_main_option("sqlalchemy.url"),
target_metadata=target_metadata,
literal_binds=True,
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_async_migrations())

View File

@@ -0,0 +1,27 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from __future__ import annotations
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
${imports if imports else ""}
revision: str = ${repr(up_revision)}
down_revision: str | None = ${repr(down_revision)}
branch_labels: str | Sequence[str] | None = ${repr(branch_labels)}
depends_on: str | Sequence[str] | None = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

View File

@@ -0,0 +1,366 @@
"""initial schema 16 MVP tables
Revision ID: 220ca2e3d53f
Revises:
Create Date: 2026-06-18 07:55:55.972365
"""
from __future__ import annotations
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "220ca2e3d53f"
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"users",
sa.Column("email", sa.Text(), nullable=False),
sa.Column("display_name", sa.Text(), nullable=True),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("email"),
)
op.create_table(
"projects",
sa.Column("owner_id", sa.Uuid(), nullable=False),
sa.Column("title", sa.Text(), nullable=False),
sa.Column("genre", sa.Text(), nullable=True),
sa.Column("logline", sa.Text(), nullable=True),
sa.Column("premise", sa.Text(), nullable=True),
sa.Column("theme", sa.Text(), nullable=True),
sa.Column(
"selling_points",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column("structure", sa.Text(), nullable=True),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(
["owner_id"],
["users.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"skills",
sa.Column("scope", sa.Text(), nullable=False),
sa.Column("owner_id", sa.Uuid(), nullable=True),
sa.Column("name", sa.Text(), nullable=False),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("tier", sa.Text(), nullable=False),
sa.Column("system_prompt", sa.Text(), nullable=False),
sa.Column("input_schema", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("output_schema", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column(
"reads",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column(
"writes",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column("genre", sa.Text(), nullable=True),
sa.Column(
"examples",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(
["owner_id"],
["users.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"chapter_digests",
sa.Column("project_id", sa.Uuid(), nullable=False),
sa.Column("chapter_no", sa.Integer(), nullable=False),
sa.Column("facts", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_chapter_digests_project_id"), "chapter_digests", ["project_id"], unique=False
)
op.create_table(
"chapter_reviews",
sa.Column("project_id", sa.Uuid(), nullable=False),
sa.Column("chapter_no", sa.Integer(), nullable=False),
sa.Column("chapter_version", sa.Integer(), nullable=True),
sa.Column(
"conflicts",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column(
"foreshadow_sug",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column("style", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("pace", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("health_score", sa.Integer(), nullable=True),
sa.Column("decisions", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_chapter_reviews_project_id"), "chapter_reviews", ["project_id"], unique=False
)
op.create_table(
"chapters",
sa.Column("project_id", sa.Uuid(), nullable=False),
sa.Column("volume", sa.Integer(), nullable=False),
sa.Column("chapter_no", sa.Integer(), nullable=False),
sa.Column("content", sa.Text(), nullable=True),
sa.Column("status", sa.Text(), server_default=sa.text("'draft'"), nullable=False),
sa.Column("version", sa.Integer(), server_default=sa.text("1"), nullable=False),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("project_id", "chapter_no", "version"),
)
op.create_index(op.f("ix_chapters_project_id"), "chapters", ["project_id"], unique=False)
op.create_table(
"characters",
sa.Column("project_id", sa.Uuid(), nullable=False),
sa.Column("name", sa.Text(), nullable=False),
sa.Column("role", sa.Text(), nullable=True),
sa.Column("traits", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("appearance", sa.Text(), nullable=True),
sa.Column("motive", sa.Text(), nullable=True),
sa.Column("backstory", sa.Text(), nullable=True),
sa.Column("arc", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("speech_tics", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column(
"tags",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column(
"relations",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column("first_chapter", sa.Integer(), nullable=True),
sa.Column("latest_state", sa.Text(), nullable=True),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_characters_project_id"), "characters", ["project_id"], unique=False)
op.create_table(
"foreshadow",
sa.Column("project_id", sa.Uuid(), nullable=False),
sa.Column("code", sa.Text(), nullable=False),
sa.Column("title", sa.Text(), nullable=False),
sa.Column("status", sa.Text(), server_default=sa.text("'OPEN'"), nullable=False),
sa.Column("planted_at", sa.Integer(), nullable=True),
sa.Column("content", sa.Text(), nullable=True),
sa.Column("expected_close_from", sa.Integer(), nullable=True),
sa.Column("expected_close_to", sa.Integer(), nullable=True),
sa.Column("importance", sa.Text(), nullable=True),
sa.Column(
"links",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column(
"progress",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("project_id", "code"),
)
op.create_index(op.f("ix_foreshadow_project_id"), "foreshadow", ["project_id"], unique=False)
op.create_table(
"jobs",
sa.Column("project_id", sa.Uuid(), nullable=True),
sa.Column("kind", sa.Text(), nullable=False),
sa.Column("status", sa.Text(), server_default=sa.text("'queued'"), nullable=False),
sa.Column("progress", sa.Integer(), server_default=sa.text("0"), nullable=False),
sa.Column("result", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("error", sa.Text(), nullable=True),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"outline",
sa.Column("project_id", sa.Uuid(), nullable=False),
sa.Column("volume", sa.Integer(), nullable=False),
sa.Column("chapter_no", sa.Integer(), nullable=False),
sa.Column("beats", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column(
"foreshadow_windows",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("project_id", "chapter_no"),
)
op.create_index(op.f("ix_outline_project_id"), "outline", ["project_id"], unique=False)
op.create_table(
"provider_credentials",
sa.Column("owner_id", sa.Uuid(), nullable=False),
sa.Column("project_id", sa.Uuid(), nullable=True),
sa.Column("provider", sa.Text(), nullable=False),
sa.Column("api_key_enc", sa.LargeBinary(), nullable=False),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["owner_id"], ["users.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("owner_id", "project_id", "provider"),
)
op.create_table(
"rules",
sa.Column("project_id", sa.Uuid(), nullable=True),
sa.Column("level", sa.Text(), nullable=False),
sa.Column("content", sa.Text(), nullable=False),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"style_fingerprint",
sa.Column("project_id", sa.Uuid(), nullable=False),
sa.Column("dimensions_json", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("evidence_json", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("version", sa.Integer(), server_default=sa.text("1"), nullable=False),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_style_fingerprint_project_id"), "style_fingerprint", ["project_id"], unique=False
)
op.create_table(
"tier_routing",
sa.Column("project_id", sa.Uuid(), nullable=True),
sa.Column("tier", sa.Text(), nullable=False),
sa.Column("provider", sa.Text(), nullable=False),
sa.Column("model", sa.Text(), nullable=False),
sa.Column(
"fallback",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'[]'"),
nullable=False,
),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("project_id", "tier"),
)
op.create_table(
"usage_ledger",
sa.Column("owner_id", sa.Uuid(), nullable=False),
sa.Column("project_id", sa.Uuid(), nullable=True),
sa.Column("provider", sa.Text(), nullable=False),
sa.Column("model", sa.Text(), nullable=False),
sa.Column("input_tokens", sa.Integer(), nullable=False),
sa.Column("output_tokens", sa.Integer(), nullable=False),
sa.Column("cache_read", sa.Integer(), server_default=sa.text("0"), nullable=False),
sa.Column("cost_minor", sa.BigInteger(), nullable=False),
sa.Column("currency", sa.Text(), nullable=False),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(
["owner_id"],
["users.id"],
),
sa.ForeignKeyConstraint(
["project_id"],
["projects.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"world_entities",
sa.Column("project_id", sa.Uuid(), nullable=False),
sa.Column("type", sa.Text(), nullable=False),
sa.Column("name", sa.Text(), nullable=False),
sa.Column("rules", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("first_chapter", sa.Integer(), nullable=True),
sa.Column("latest_state", sa.Text(), nullable=True),
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_world_entities_project_id"), "world_entities", ["project_id"], unique=False
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_world_entities_project_id"), table_name="world_entities")
op.drop_table("world_entities")
op.drop_table("usage_ledger")
op.drop_table("tier_routing")
op.drop_index(op.f("ix_style_fingerprint_project_id"), table_name="style_fingerprint")
op.drop_table("style_fingerprint")
op.drop_table("rules")
op.drop_table("provider_credentials")
op.drop_index(op.f("ix_outline_project_id"), table_name="outline")
op.drop_table("outline")
op.drop_table("jobs")
op.drop_index(op.f("ix_foreshadow_project_id"), table_name="foreshadow")
op.drop_table("foreshadow")
op.drop_index(op.f("ix_characters_project_id"), table_name="characters")
op.drop_table("characters")
op.drop_index(op.f("ix_chapters_project_id"), table_name="chapters")
op.drop_table("chapters")
op.drop_index(op.f("ix_chapter_reviews_project_id"), table_name="chapter_reviews")
op.drop_table("chapter_reviews")
op.drop_index(op.f("ix_chapter_digests_project_id"), table_name="chapter_digests")
op.drop_table("chapter_digests")
op.drop_table("skills")
op.drop_table("projects")
op.drop_table("users")
# ### end Alembic commands ###