mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-02 07:59:31 +08:00
- 新增 server-only 依赖,在 19 个服务端模块首行加 import "server-only"; 覆盖直接导入 @prisma/client、Node 内置模块(fs/path/crypto)、 next-auth getServerSession 的模块,以及 prisma/logger/auth/api-handler/ config\/store\/ai\/index\/ai\/base-provider 等核心服务端文件 - vitest.config.mts 配置 server-only 别名指向空模块桩, 避免 jsdom 环境导入真实 server-only 抛错 - AGENTS.md 代码规范补充 server-only 使用约定 检查结论:217 个客户端文件(含 'use client' 及 components/contexts/app 下) 经脚本递归检查,无任何文件直接或间接导入服务端模块; scripts/ 与 prisma/ 下无脚本导入 src/lib,不受 server-only 影响。
63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
|
||
/**
|
||
* Gemini 接口测试脚本
|
||
* 功能:
|
||
* 1. 实例化 GeminiProvider。
|
||
* 2. 测试 generateSimilarQuestion (生成相似题) 功能。
|
||
* 3. 测试 analyzeImage (图片分析) 功能。
|
||
* 运行:npx tsx --conditions=react-server scripts/test-ai-gemini.ts(提供商模块带 server-only 保护,需以 react-server 条件解析)
|
||
* 用途:用于验证 Gemini API 配置是否正确以及核心功能是否可用。
|
||
*/
|
||
import { GeminiProvider } from "../src/lib/ai/gemini-provider";
|
||
import dotenv from "dotenv";
|
||
import path from "path";
|
||
|
||
// Load environment variables from .env file in root
|
||
dotenv.config({ path: path.resolve(__dirname, "../.env") });
|
||
|
||
async function testGemini() {
|
||
console.log("Testing Gemini Connection...");
|
||
console.log("API Key present:", !!process.env.GOOGLE_API_KEY);
|
||
|
||
if (!process.env.GOOGLE_API_KEY) {
|
||
console.error("Error: GOOGLE_API_KEY is missing in .env");
|
||
return;
|
||
}
|
||
|
||
const provider = new GeminiProvider();
|
||
|
||
try {
|
||
console.log("Sending generateSimilarQuestion request...");
|
||
const result = await provider.generateSimilarQuestion(
|
||
"What is 1 + 1?",
|
||
["Math"],
|
||
"en",
|
||
"easy"
|
||
);
|
||
console.log("generateSimilarQuestion Success!");
|
||
} catch (error: any) {
|
||
console.error("generateSimilarQuestion Failed:", error.message);
|
||
}
|
||
|
||
try {
|
||
console.log("Sending analyzeImage request with REAL image...");
|
||
const fs = await import("fs");
|
||
const imagePath = "/home/wttwins/.gemini/antigravity/brain/2a8874fc-faf8-4cb4-8137-053b1c27b811/uploaded_image_1764336004527.png";
|
||
|
||
if (fs.existsSync(imagePath)) {
|
||
const imageBuffer = fs.readFileSync(imagePath);
|
||
const base64Image = imageBuffer.toString("base64");
|
||
|
||
const result = await provider.analyzeImage(base64Image, "image/png");
|
||
console.log("analyzeImage Success! Response:", JSON.stringify(result, null, 2));
|
||
} else {
|
||
console.error("Test image not found at:", imagePath);
|
||
}
|
||
} catch (error: any) {
|
||
console.error("analyzeImage Failed!");
|
||
console.error("Error Message:", error.message);
|
||
}
|
||
}
|
||
|
||
testGemini();
|