Skip to main content

Precis 后端 / Precis Backend

Alpha — 核心功能已成型,接口可能调整。

FastAPI + CLI + 核心校验引擎,采用三层分离架构。

完整项目说明请见 根目录 README.md。


架构

backend/app/
├── api/                    # API 层
│   ├── main.py             # FastAPI 应用入口(路由注册、中间件、CORS)
│   ├── dependencies.py     # 依赖注入
│   ├── middleware/          # 中间件
│   ├── models/             # 请求/响应 Pydantic 模型
│   ├── routers/            # 路由定义
│   │   ├── core/           # 核心路由(项目、工作区)
│   │   ├── project/        # V2 项目 CRUD(Schema、Constraint、Regex、Transform)
│   │   ├── validation/     # 校验执行与历史
│   │   ├── preview/        # 数据预览
│   │   └── ai/             # AI 配置生成
│   └── services/           # API 层服务
├── cli/                    # 交互式命令行(shell/ REPL、start 启动器、__main__)
├── shared/                 # 三层分离架构
│   ├── core/               # 框架级基础设施
│   │                       # 文件 I/O、配置解析(YAML)、数据加载
│   ├── domain/             # 纯业务领域逻辑(无 I/O 依赖)
│   │   ├── constraints/    # 10 种约束类型定义(not_null.py、unique.py 等,每种一个文件)
│   │   ├── transforms/     # 22 种转换类型定义(string_split.py、math_expr.py 等,每种一个文件)
│   │   ├── data_types.py   # 数据类型定义(string/integer/float/decimal/boolean/date)
│   │   ├── dataset_schema.py # Schema 模型
│   │   ├── expression_system.py # 表达式求值系统
│   │   └── schema/         # Schema 相关领域逻辑
│   └── services/           # 应用服务(编排 core 和 domain)
│       ├── validation/     # 校验引擎(两阶段流水线)
│       │   ├── executor.py # ValidationExecutor 主编排器
│       │   ├── engine.py   # 校验执行引擎
│       │   ├── data_loader.py # 数据加载
│       │   ├── loader.py   # 配置加载
│       │   ├── resolver.py # 数据源解析
│       │   ├── extractors.py # 派生列提取(regex)
│       │   ├── history.py  # 校验历史持久化
│       │   ├── dag/        # 转换 DAG 执行
│       │   ├── validators/ # 各类型校验器(每种约束一个文件)
│       │   └── types.py    # 校验类型定义
│       ├── ai/             # AI 服务
│       ├── llm/            # LLM 集成(OpenAI / Ollama)
│       ├── preview/        # 数据预览服务
│       ├── diff/           # 配置差异比较
│       └── hardware.py     # 硬件检测
└── start_server.py         # 服务器启动入口

关键约定

  • domain/ 不得导入 core/ 或 services/,保持纯净
  • API 路由在 api/routers/,请求/响应模型在 api/models/
  • 路由注册入口:api/main.py
  • 所有请求通过 X-Project-Config-Path header 标识当前项目
  • 校验类接口约定"永远返回 200、结果看 body";配置文件损坏(如清单 YAML 语法错误/空文件)返回 422 并附说明
  • 打包模式下 Origin: null 跨域请求须携带 X-Precis-Auth 头(值来自 Electron 注入的 PRECIS_API_TOKEN,见 api/middleware/token_auth.py);未配置 token 时中间件直通,PRECIS_ALLOW_NULL_ORIGIN=1 为旧的全局放行兼容开关

校验引擎(两阶段流水线)

阶段 1: 数据加载与预处理
  ├── DataSourceResolver → 解析文件路径
  ├── DataLoader → 加载 Excel/CSV/JSON
  ├── process_dataframe → 类型转换、格式检查
  ├── extractors → 派生列提取(regex)
  └── Transform DAG → 拓扑排序执行 transform 链

阶段 2: 约束校验
  └── 逐约束调用 validate(),聚合错误
        (validators/ 下每种类型一个:not_null.py, unique.py, foreign_key.py ...)

开发命令

# 安装
python -m venv .venv
pip install -e ".[dev]"

# 运行
python -m uvicorn app.api.main:app --reload --port 18000

# 代码检查
python -m ruff check .              # lint(不自动修复)
python -m ruff check --fix .        # lint 自动修复
python -m ruff format .             # 格式化
python -m mypy .                    # 类型检查

# 测试
python -m pytest                    # 运行全部测试

# CLI
python -B -m app.cli

配置文件格式(V2 YAML)

文件类型 命名 说明
项目清单 project.precis.yaml 索引所有 Schema/Constraint/Regex/Transform 资源
Schema *.schema.yaml 表结构定义(列、数据类型、内嵌约束)
Constraint *.constraint.yaml 独立约束(refs + params 分离设计)
Regex *.regex.yaml 正则节点(引用模式或直接模式)

Release files for precis-cli 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for precis-cli 0.1.3
File Size Uploaded
precis_cli-0.1.3.tar.gz 772.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for precis-cli 0.1.3
File Interpreter ABI Platform
precis_cli-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 2.0 MB

Release files / precis_cli-0.1.3.tar.gz

Download URL precis_cli-0.1.3.tar.gz
Size 772.1 kB
Tags Source
SHA-256 checksum
How to use checksums
31e62f98dea81b0fd8a495ed982876267ca297cca6f8ccd53f4c9cdf0e140494
BLAKE2b-256 checksum
How to use checksums
5976e9ca9df3e32a20bcee4bbcb23f84165520454ba5911485b4e14b4b581aee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 20, 2026.

Transparency log

Release files / precis_cli-0.1.3-py3-none-any.whl

Download URL precis_cli-0.1.3-py3-none-any.whl
Size 1.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
dccbb05a83b707fbbcd952db18e79d1db57b1e73b25163e20230d5f4247ba2b2
BLAKE2b-256 checksum
How to use checksums
35a188d3e49b5cb6d80f770955eb441a9896423d57c29955cf701382affd226e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 20, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

This release

0.1.3 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page