diff --git a/AGENTS.md b/AGENTS.md index 285a61f7..4fb479ce 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -61,7 +61,7 @@ bash scripts/ci.sh # 完整 CI(要求 Node.js 22 及以上,含生 - 新路由使用 `@/lib/api-handler` 的 `withUser`/`withAdmin` 处理鉴权(自动完成会话校验、用户查询与异常捕获),与 `withRecoverableResponse` 组合时后者放外层。 - 界面文本放在 `@/lib/translations.ts`(入口,实际文案按语言拆分在 `src/lib/i18n/en.ts` / `src/lib/i18n/zh.ts`),组件中用 `t.section.key`,缺省回退英文。中英文 key 必须一致(`translations.test.ts` 会检查)。 - 页面须同时适配 PC 和移动端(移动优先)。 -- 修改 AI 调用时同步更新所有提供商(`gemini-provider.ts`、`openai-provider.ts`、`azure-provider.ts`、`anthropic-provider.ts`),提示词集中在 `prompts/` 目录。 +- AI 业务方法统一实现在 `src/lib/ai/base-provider.ts` 的 `BaseAIProvider`:新增或修改 AI 功能只改基类(及 `prompts/`、解析函数),各提供商(`openai-provider.ts`、`azure-provider.ts`、`gemini-provider.ts`、`anthropic-provider.ts`)只负责 `complete` 传输与按任务的请求差异;回答解析在 `response-parser.ts`,错误归类在 `error-classification.ts`。提示词集中在 `prompts/` 目录。 - AI 调用不要写死 `max_tokens`:OpenAI、Azure、Anthropic 一律经 `withModelOutputLimit`(`src/lib/ai/truncation.ts`)按模型最大输出请求(已知模型查 `model-output-limits.ts`,未知模型默认 256K,超限时自动降级);新增模型的官方输出上限补到 `model-output-limits.ts`。推理模型的思考也计入输出,小上限会导致正文为空。 - MiniMax、智谱 GLM、DeepSeek、Kimi、千问 Qwen、阶跃星辰 StepFun、火山引擎豆包等国产大模型官方渠道复用 `OpenAIProvider`,默认地址和模型集中在 `src/lib/ai/vendor-presets.ts`;新增渠道只需加预设并同步文档与测试。 diff --git a/doc/FORK_EXTENSION_POINTS.md b/doc/FORK_EXTENSION_POINTS.md index 9aad8d02..dede4027 100644 --- a/doc/FORK_EXTENSION_POINTS.md +++ b/doc/FORK_EXTENSION_POINTS.md @@ -11,7 +11,7 @@ fork 项目可在此文件中: - **增删导航项**:编辑 `NAV_ITEMS` 数组。每个导航项包含 `key`(对应 `translations.ts` 中 `t.nav` 的翻译 key)、`href`、`icon`(lucide 图标)与 `match(pathname)` 高亮规则。 - **修改无导航路径**:编辑 `BARE_PATHS` 正则数组(登录、注册、打印预览等页面不显示全局导航)。 - **替换品牌图标**:修改 `APP_BRAND.iconSrc`,无需改动 `src/components/app-shell.tsx`。 -- **新增翻译 key**:在 `src/lib/translations.ts` 的 `nav` 节(中英文同步)中添加对应 key。 +- **新增翻译 key**:在 `src/lib/i18n/zh.ts` 与 `src/lib/i18n/en.ts` 的 `nav` 节(中英文同步)中添加对应 key。 移动端底部导航的列数按 `NAV_ITEMS.length` 自动计算,无需手工调整样式。 diff --git a/doc/PROJECT_OVERVIEW.md b/doc/PROJECT_OVERVIEW.md index 3591ca36..ecfe6c3f 100644 --- a/doc/PROJECT_OVERVIEW.md +++ b/doc/PROJECT_OVERVIEW.md @@ -87,7 +87,8 @@ wrongNotebook/ │ │ │ ├── index.ts # 工厂函数 (getAIService) │ │ │ ├── types.ts # AIService 接口定义 │ │ │ ├── schema.ts # Zod 校验 (ParsedQuestionSchema) -│ │ │ ├── prompts.ts # 提示词模板 (分析/相似题/重新解题) +│ │ │ ├── base-provider.ts # 提供商公共基类(14 个业务方法) +│ │ │ ├── prompts/ # 提示词模板(按任务拆分) │ │ │ ├── gemini-provider.ts # Google Gemini Provider │ │ │ ├── openai-provider.ts # OpenAI Provider │ │ │ ├── azure-provider.ts # Azure OpenAI Provider diff --git a/doc/TELEMETRY_DESIGN.md b/doc/TELEMETRY_DESIGN.md index 73c51ada..0992a807 100644 --- a/doc/TELEMETRY_DESIGN.md +++ b/doc/TELEMETRY_DESIGN.md @@ -53,7 +53,7 @@ flowchart LR ## 1. 现状事实(调研结论) ### 1.1 配置与设置 -- 应用级配置在 `config/app-config.json`:`src/lib/config.ts` 中的 `AppConfig`(L29)、`DEFAULT_CONFIG`(L124)、`getAppConfig`(L215)、`updateAppConfig`(L267)。写入用临时文件加 rename;嵌套对象要深合并,可参照 `mergeTtsConfig`。 +- 应用级配置在 `config/app-config.json`:`src/lib/config/` 中的 `AppConfig`(`types.ts`)、`DEFAULT_CONFIG`(`defaults.ts`)、`getAppConfig` 与 `updateAppConfig`(`store.ts`)。写入用临时文件加 rename;嵌套对象要深合并,可参照 `mergeTtsConfig`。 - 管理员接口的惯例:`getServerSession` + `requireAdmin(session)`,不通过时返回 `forbidden()`(例:`src/app/api/settings/route.ts` 的 POST)。 - 管理员设置分区的先例:`src/components/settings/debug-log-settings.tsx`,在 `settings-dialog.tsx` L191 注册。 - 版本号取自 `src/lib/app-version.ts`,优先使用构建时注入的 `APP_VERSION`。 @@ -528,7 +528,7 @@ model DailyUsage { - 测试:`unit/telemetry-schema`、`telemetry-sanitize`、`telemetry-client`、`ingest-contract`、`eula`;`integration/telemetry-route`、`telemetry-outbox`、`admin-telemetry-route`、`eula-routes`;`e2e/eula-and-telemetry.spec.ts` **修改** -- `src/lib/config.ts`、`prisma/schema.prisma`、`src/lib/rate-limit.ts`、`src/lib/api-errors.ts` +- `src/lib/config/`、`prisma/schema.prisma`、`src/lib/rate-limit.ts`、`src/lib/api-errors.ts` - 高价值路由的 catch 块、`src/lib/frontend-logger.ts`(全局监听追加上报)、`src/instrumentation.ts`(外发队列和清理) - `src/app/api/initialize/route.ts`、`src/components/initialize/initialize-form.tsx` - `src/lib/initial-admin.ts`、`src/lib/initialization.ts` diff --git a/doc/wiki/AI-Configuration.md b/doc/wiki/AI-Configuration.md index 208b3015..14a1c37a 100644 --- a/doc/wiki/AI-Configuration.md +++ b/doc/wiki/AI-Configuration.md @@ -125,4 +125,4 @@ GeoGebra 的文字、不等式和标签参数按共用命令清单校验,重 API Key 保存在服务端配置中。备份 `config/` 时应妥善保存,反馈问题时只提供经过脱敏的配置、日志和错误信息。 -实现入口:[配置读取](https://github.com/margrop/wrong-notebook/blob/main/src/lib/config.ts) · [提供商选择](https://github.com/margrop/wrong-notebook/blob/main/src/lib/ai/index.ts) · [提示词](https://github.com/margrop/wrong-notebook/blob/main/src/lib/ai/prompts.ts)。 +实现入口:[配置读取](https://github.com/margrop/wrong-notebook/tree/main/src/lib/config) · [提供商选择](https://github.com/margrop/wrong-notebook/blob/main/src/lib/ai/index.ts) · [提示词](https://github.com/margrop/wrong-notebook/tree/main/src/lib/ai/prompts)。 diff --git a/doc/wiki/Development.md b/doc/wiki/Development.md index 7f0f61c9..c0fda95d 100644 --- a/doc/wiki/Development.md +++ b/doc/wiki/Development.md @@ -10,12 +10,12 @@ | --- | --- | | `src/app/` | 页面与 API 路由 | | `src/components/` | 业务组件和基础 UI | -| `src/lib/ai/` | 四家 AI 提供商、提示词、结果结构与校验 | -| `src/lib/config.ts` | 全局设置与配置文件读取 | +| `src/lib/ai/` | AI 提供商公共基类与四家提供商、提示词(`prompts/`)、结果结构与校验 | +| `src/lib/config/` | 全局设置与配置文件读取 | | `src/lib/question-deduplication.ts` | 导入判重与重复作答记录 | | `src/lib/mastery.ts`、`src/lib/exam.ts` | 掌握评估与试卷生成 | | `src/lib/date-time.ts` | 应用时区、显示与统计边界 | -| `src/lib/translations.ts` | 界面文本 | +| `src/lib/translations.ts`、`src/lib/i18n/` | 界面文本(入口与按语言拆分的文案) | | `prisma/` | 数据模型、正式迁移和初始化数据 | | `src/__tests__/`、`e2e/`、`tests/publication/` | 单元、集成、E2E 与公开发布检查测试 | | `scripts/`、`doc/` | 开发维护脚本与专项文档 | @@ -75,7 +75,7 @@ fork 项目(例如「AI 题库」)可通过上游预留的扩展点定制功 ## 修改 AI 功能 -AI 逻辑变化需同步考虑 Gemini、OpenAI、Azure、Anthropic 四个提供商。提示词集中在 `prompts.ts`;生成题目的题型与格式契约还由 `generated-question.ts` 等模块校验。 +AI 业务方法统一实现在 `src/lib/ai/base-provider.ts`,Gemini、OpenAI、Azure、Anthropic 四个提供商只实现请求传输与各自的请求差异,新增 AI 功能只需修改基类。提示词集中在 `prompts/` 目录;生成题目的题型与格式契约还由 `generated-question.ts` 等模块校验。 验证时覆盖正常结果、格式异常、超时和输出截断。变更输出结构后,也要检查练习、试卷、配图和重新解答等调用方。 diff --git a/doc/wiki/en/AI-Configuration-en.md b/doc/wiki/en/AI-Configuration-en.md index db20edad..e9ab4a49 100644 --- a/doc/wiki/en/AI-Configuration-en.md +++ b/doc/wiki/en/AI-Configuration-en.md @@ -125,4 +125,4 @@ Knowledge points are chosen from the tag library matching the notebook's school API keys are stored in the server configuration. Keep backups of `config/` safe, and only share redacted configuration, logs and error messages when reporting problems. -Code entry points: [configuration](https://github.com/margrop/wrong-notebook/blob/main/src/lib/config.ts) · [provider selection](https://github.com/margrop/wrong-notebook/blob/main/src/lib/ai/index.ts) · [prompts](https://github.com/margrop/wrong-notebook/blob/main/src/lib/ai/prompts.ts). +Code entry points: [configuration](https://github.com/margrop/wrong-notebook/tree/main/src/lib/config) · [provider selection](https://github.com/margrop/wrong-notebook/blob/main/src/lib/ai/index.ts) · [prompts](https://github.com/margrop/wrong-notebook/tree/main/src/lib/ai/prompts). diff --git a/doc/wiki/en/Development-en.md b/doc/wiki/en/Development-en.md index ecf07062..2b44d84f 100644 --- a/doc/wiki/en/Development-en.md +++ b/doc/wiki/en/Development-en.md @@ -11,11 +11,11 @@ The project uses the Next.js 16 App Router, React 19, TypeScript, Tailwind CSS 4 | `src/app/` | Pages and API routes | | `src/components/` | Feature components and base UI | | `src/lib/ai/` | AI providers, prompts, result schemas and validation | -| `src/lib/config.ts` | Global settings and configuration file access | +| `src/lib/config/` | Global settings and configuration file access | | `src/lib/question-deduplication.ts` | Import duplicate detection and repeated-attempt records | | `src/lib/mastery.ts`, `src/lib/exam.ts` | Mastery evaluation and exam generation | | `src/lib/date-time.ts` | App time zone, display and statistics boundaries | -| `src/lib/translations.ts` | Interface text | +| `src/lib/translations.ts`, `src/lib/i18n/` | Interface text (entry point and per-language strings) | | `prisma/` | Data model, formal migrations and seed data | | `src/__tests__/`, `e2e/`, `tests/publication/` | Unit, integration, E2E and publication check tests | | `scripts/`, `doc/` | Maintenance scripts and topic documents | @@ -75,7 +75,7 @@ Fork projects (for example an "AI question bank") can customize features through ## Changing AI features -AI logic changes must consider all providers: Gemini, OpenAI (including the MiniMax, GLM, DeepSeek, Kimi, Qwen, StepFun and Volcengine channels), Azure and Anthropic. Prompts live in `prompts.ts`; the type and format contract of generated questions is also validated by modules such as `generated-question.ts`. +AI business methods are implemented once in `src/lib/ai/base-provider.ts`. The providers — Gemini, OpenAI (including the MiniMax, GLM, DeepSeek, Kimi, Qwen, StepFun and Volcengine channels), Azure and Anthropic — only implement request transport and their own request differences, so a new AI feature only changes the base class. Prompts live in the `prompts/` directory; the type and format contract of generated questions is also validated by modules such as `generated-question.ts`. Cover normal results, malformed output, timeouts and truncated output when verifying. After changing an output structure, also check callers such as practice, exam papers, figures and re-solving. diff --git a/scripts/prepare-e2e.js b/scripts/prepare-e2e.js index 13ca9fdb..4f76ec90 100644 --- a/scripts/prepare-e2e.js +++ b/scripts/prepare-e2e.js @@ -13,7 +13,7 @@ const { hash } = require('bcryptjs'); const E2E_ADMIN_PASSWORD = process.env.E2E_ADMIN_PASSWORD || 'e2e-admin-pass'; const DEFAULT_ADMIN_PASSWORD = '123456'; const SETUP_ADMIN_EMAIL = 'setup-admin@localhost'; -// 与 src/lib/config.ts 一致 +// 与 src/lib/config/ 一致 const CONFIG_FILE_PATH = path.join(process.env.CONFIG_DIR || path.join(process.cwd(), 'config'), 'app-config.json'); // 直接读取并处理不存在的情况,避免先判断再读写之间文件被替换