设置
前置条件:Python 3.11+、uv、Node.js 18+、pnpm。LLM_API_KEY 仅在本地运行后端或在提交时预览自动翻译的区域设置输出时需要。没有密钥的贡献者仍然可以提交英文更改 — GitHub Actions 工作流在 PR 合并后在 master 上进行翻译。请参阅下面的 i18n。类型安全
mypy 严格模式已启用。整个代码库通过零错误检查,预提交钩子在每个暂存的.py 文件上运行 mypy,并进行完整的导入链检查。
规则:
- 所有公共函数都需要类型提示。
- 除了
import-untyped(没有存根的第三方库)外,不允许使用# type: ignore。 - 使用
from __future__ import annotations来处理前向引用。 - 修复实际类型——永远不要通过抑制错误来让 CI 通过。
测试
每个新模块必须有对应的测试文件。每个功能提交必须包含测试。
规则:
- 提交前所有现有测试必须通过:
uv run pytest tests/ -x -q。 - 测试不得依赖外部服务 — 使用
unittest.mock/AsyncMock模拟数据库、MCP 服务器、HTTP 端点和 LLM 调用。 - 运行带覆盖率的测试:
uv run pytest --cov=fim_one。
代码风格
- 异步优先。 对于 I/O 绑定操作使用
async def。用asyncio.to_thread()包装同步调用,而不是阻塞事件循环。 - 使用 Ruff 进行代码检查。
uv run ruff check src/— 行长度为 100 个字符。 - 最小化
__init__.py导出。 仅重新导出公共 API;保持内部符号私有。 - 包管理器。 Python 使用
uv,前端使用pnpm。永远不要使用pip或npm。
Git 约定
原子提交。 每次提交包含一个逻辑变更。即使是一起开发的无关变更也要分开提交。 提交信息格式:type: description
不要跳过钩子 使用
--no-verify。预提交管道的存在是为了在错误进入仓库之前捕获它们。
Pre-commit hook pipeline
The hook runs automatically on every commit. It only processes staged files relevant to each step — if you only changed Python files, the i18n step is skipped, and vice versa.
If any step fails, the commit is aborted. Fix the reported errors and retry.
i18n
FIM One supports 6 locales (EN, ZH, JA, KO, DE, FR). Translations are fully automated. Only edit English source files:frontend/messages/en/{namespace}.json— UI stringsdocs/*.mdx(root level, not locale subdirs) — documentationREADME.md— project readme
messages/zh/, docs/zh/, README.zh.md, etc.) are auto-generated and the pre-commit hook refuses commits that manually edit them. To fix a mistranslation, edit scripts/translation-glossary.md — the prompt-injected single source of truth for every translation rule — then regenerate affected files with uv run scripts/translate.py --files <en-sources> --force. Every glossary fix applies to all five locales permanently.
Two paths to generating locale files:
- Local pre-commit hook — runs if
LLM_API_KEYis set in.env. Translation happens before push, so you can preview the output. - CI fallback — if the local hook had no key and skipped,
.github/workflows/i18n-sync.ymltranslates onmasterafter merge and auto-commits the result.
messages/en/{ns}.json — other locale files are generated on the next commit or on CI after merge.
Force full retranslation: uv run scripts/translate.py --all (requires local LLM_API_KEY).
数据库迁移
开发环境使用 SQLite,生产环境使用 PostgreSQL。一套 Alembic 迁移文件必须在两者上都能工作。 关键规则:- 每个新的 ORM 模型或列必须有一个迁移 — 永远不要依赖
metadata.create_all()。 - 所有迁移必须是幂等的 — 使用
fim_one.migrations.helpers中的辅助函数(table_exists、table_has_column、index_exists)。 - 布尔默认值:
server_default=sa.text("FALSE")/sa.text("TRUE")。永远不要使用"0"/"1"— PostgreSQL 拒绝为布尔列使用整数字面量。 - SQLite 无法执行
ALTER COLUMN— 对于列约束更改,使用op.batch_alter_table()。 - 编写迁移后,立即运行
uv run alembic upgrade head来应用它。