mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-05 00:30:05 +08:00
增加首页举一反三统计信息
This commit is contained in:
53
scripts/test-gemini.ts
Normal file
53
scripts/test-gemini.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
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();
|
||||
@@ -21,6 +21,11 @@ export function MarkdownRenderer({ content, className = '' }: MarkdownRendererPr
|
||||
.replace(/([.!?;])\s*\n(?!\n)/g, '$1\n\n') // English punctuation followed by single newline
|
||||
.replace(/(\d+\))\s*\n(?!\n)/g, '$1\n\n') // Numbered items like (1), (2)
|
||||
.replace(/([\u2460-\u2473])\s*\n(?!\n)/g, '$1\n\n') // Circled numbers ①②③
|
||||
// Fix: Remove indentation for lines starting with circled numbers or (n) to prevent code block rendering
|
||||
.replace(/\n\s+([\u2460-\u2473])/g, '\n$1')
|
||||
.replace(/\n\s+(\d+\))/g, '\n$1')
|
||||
// Fix: Replace literal \n sequences with actual newlines
|
||||
.replace(/\\n/g, '\n')
|
||||
// Restore preserved double line breaks
|
||||
.replace(/\n\n__PRESERVE__\n\n/g, '\n\n');
|
||||
|
||||
|
||||
@@ -37,8 +37,9 @@ export class GeminiProvider implements AIService {
|
||||
// Try heuristic fix for LaTeX escaping
|
||||
try {
|
||||
const fixedJson = jsonString
|
||||
.replace(/\\([a-zA-Z]+)/g, '\\\\$1')
|
||||
.replace(/\\\\\\\\/g, '\\\\');
|
||||
// Fix: Only escape backslashes that are NOT followed by valid JSON escape characters (n, r, t, b, f, u, ", \)
|
||||
.replace(/\\(?![nrtbfu"\\/])/g, '\\\\')
|
||||
// The previous logic was too aggressive: .replace(/\\([a-zA-Z]+)/g, '\\\\$1')
|
||||
return JSON.parse(fixedJson) as ParsedQuestion;
|
||||
} catch (secondError) {
|
||||
console.error("JSON parse failed:", secondError);
|
||||
|
||||
@@ -43,8 +43,8 @@ export class OpenAIProvider implements AIService {
|
||||
} catch (error) {
|
||||
try {
|
||||
const fixedJson = jsonString
|
||||
.replace(/\\([a-zA-Z]+)/g, '\\\\$1')
|
||||
.replace(/\\\\\\\\/g, '\\\\');
|
||||
// Fix: Only escape backslashes that are NOT followed by valid JSON escape characters (n, r, t, b, f, u, ", \)
|
||||
.replace(/\\(?![nrtbfu"\\/])/g, '\\\\');
|
||||
return JSON.parse(fixedJson) as ParsedQuestion;
|
||||
} catch (secondError) {
|
||||
console.error("JSON parse failed:", secondError);
|
||||
|
||||
Reference in New Issue
Block a user