mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-07 06:37:09 +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 影响。
85 lines
3.2 KiB
TypeScript
85 lines
3.2 KiB
TypeScript
|
|
/**
|
|
* OpenAI 接口测试脚本
|
|
* 功能:
|
|
* 1. 实例化 OpenAIProvider。
|
|
* 2. 测试 generateSimilarQuestion (生成相似题) 功能。
|
|
* 3. 测试 analyzeImage (图片分析) 功能。
|
|
* 运行:npx tsx --conditions=react-server scripts/test-ai-openai.ts(提供商模块带 server-only 保护,需以 react-server 条件解析)
|
|
* 用途:用于验证 OpenAI (或兼容接口) 配置是否正确以及核心功能是否可用。
|
|
*/
|
|
import { OpenAIProvider } from "../src/lib/ai/openai-provider";
|
|
import dotenv from "dotenv";
|
|
import path from "path";
|
|
import fs from "fs";
|
|
|
|
// Load environment variables from .env file in root
|
|
dotenv.config({ path: path.resolve(__dirname, "../.env") });
|
|
|
|
async function testOpenAI() {
|
|
console.log("Testing OpenAI Connection...");
|
|
console.log("API Key present:", !!process.env.OPENAI_API_KEY);
|
|
console.log("Base URL:", process.env.OPENAI_BASE_URL);
|
|
console.log("Model:", process.env.OPENAI_MODEL);
|
|
|
|
if (!process.env.OPENAI_API_KEY) {
|
|
console.error("Error: OPENAI_API_KEY is missing in .env");
|
|
return;
|
|
}
|
|
|
|
// Explicitly use glm-4-flash if not set in env, or respect env
|
|
const model = process.env.OPENAI_MODEL || "glm-4-flash";
|
|
const provider = new OpenAIProvider({
|
|
model: model
|
|
});
|
|
|
|
// Test 1: Generate Similar Question
|
|
try {
|
|
console.log("\n--- Test 1: generateSimilarQuestion ---");
|
|
console.log("Sending request...");
|
|
const result = await provider.generateSimilarQuestion(
|
|
"已知 $x + y = 5$,求 $2x + 2y$ 的值。",
|
|
["代数", "因式分解"],
|
|
"zh",
|
|
"medium"
|
|
);
|
|
console.log("Success! Response:");
|
|
console.log(JSON.stringify(result, null, 2));
|
|
} catch (error: any) {
|
|
console.error("Test 1 Failed:", error.message);
|
|
if (error.cause) console.error("Cause:", error.cause);
|
|
}
|
|
|
|
// Test 2: Analyze Image
|
|
try {
|
|
console.log("\n--- Test 2: analyzeImage ---");
|
|
|
|
// Try to find a real image in the artifacts directory
|
|
const artifactDir = "/home/wttwins/.gemini/antigravity/brain/7857ca65-80c3-41fe-a5d2-21f3802af3f8";
|
|
const imageFile = "eng.jpg";
|
|
const realImagePath = path.join(artifactDir, imageFile);
|
|
|
|
let base64Image = "";
|
|
|
|
if (fs.existsSync(realImagePath)) {
|
|
console.log(`Found real test image: ${imageFile}`);
|
|
const imageBuffer = fs.readFileSync(realImagePath);
|
|
base64Image = imageBuffer.toString("base64");
|
|
} else {
|
|
console.log("No real test image found, using dummy 1x1 pixel image...");
|
|
// Creating a minimal 1x1 white pixel PNG base64
|
|
base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
|
|
}
|
|
|
|
console.log("Sending request...");
|
|
const result = await provider.analyzeImage(base64Image, "image/png", "zh");
|
|
console.log("Success! Response:");
|
|
console.log(JSON.stringify(result, null, 2));
|
|
} catch (error: any) {
|
|
console.error("Test 2 Failed:", error.message);
|
|
if (error.cause) console.error("Cause:", error.cause);
|
|
}
|
|
}
|
|
|
|
testOpenAI();
|