Files
wrong-notebook/next.config.ts
margrop af2521ffa7 refactor: AI 额度与凭据改为可替换的扩展接口
新增 AI 账户接口 AiAccountProvider(src/lib/ai/account.ts),集中四件事:调用前的额度判断、按用户的 AI 配置、每次补全的用量记录、用户删除后的清理。默认实现即原有行为:每日次数上限(管理员不受限)、全站共用 AI 配置、用量写入 AiCompletion。

- consumeAiQuota 在 AI 数据授权检查之后交给 authorize 判断,实现出错时放行并记日志;授权检查仍由上游负责,扩展不能跳过
- getAIService 先取 resolveConfig:返回 OpenAI 兼容配置时按该地址、密钥和模型请求,并发名额与 API Key 无关
- 自助注销、管理员删除用户、系统初始化删除用户成功后通知 onUserDeleted,出错不影响删除
- 构建时用 AI_ACCOUNT_EXTENSION 指定实现文件,next.config.ts 经 Turbopack resolveAlias 把 @wrong-notebook/ai-account 指向它;Vitest 固定使用默认实现
- 私有实现约定放在 src/extensions/local/,加入 .gitignore 与上传前检查的私有路径

文档补充 FORK_EXTENSION_POINTS.md 的 AI 账户一节、.env.example 与 AGENTS.md 约定;测试覆盖扩展拒绝与出错、单独配置、删除通知(含真实 SQLite)和上传前检查。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 14:18:09 +08:00

43 lines
1.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { NextConfig } from "next";
/**
* 所有响应都带的安全头。
* 不设置 HSTS(局域网常用 HTTP 访问),也不限制脚本来源(GeoGebra、KaTeX 等需要),
* 只收紧不影响功能的部分:禁止被其他站点嵌入、禁止插件和改写 base/表单目标。
*/
export const securityHeaders = [
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "X-Frame-Options", value: "SAMEORIGIN" },
{ key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
// 上传题目需要摄像头和屏幕截图,其余敏感能力一律关闭
{ key: "Permissions-Policy", value: "camera=(self), display-capture=(self), microphone=(), geolocation=(), payment=(), usb=()" },
{ key: "Content-Security-Policy", value: "frame-ancestors 'self'; base-uri 'self'; object-src 'none'; form-action 'self'" },
];
/**
* AI 账户扩展:构建时用环境变量 AI_ACCOUNT_EXTENSION(相对项目根目录的文件路径)替换默认实现,见 doc/FORK_EXTENSION_POINTS.md。
*/
export function aiAccountExtensionPath(value = process.env.AI_ACCOUNT_EXTENSION): string {
const path = value?.trim();
if (!path) return './src/lib/ai/account-default.ts';
return path.startsWith('.') || path.startsWith('/') ? path : `./${path}`;
}
const nextConfig: NextConfig = {
output: 'standalone',
turbopack: {
resolveAlias: {
'@wrong-notebook/ai-account': aiAccountExtensionPath(),
},
},
serverExternalPackages: ['@prisma/client', 'bcryptjs'],
// 禁止 next dev 自动向 AGENTS.md / CLAUDE.md 写入 agent-rules 段落
agentRules: false,
poweredByHeader: false,
async headers() {
return [{ source: "/:path*", headers: securityHeaders }];
},
};
export default nextConfig;