-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.js
More file actions
37 lines (33 loc) · 1021 Bytes
/
Copy pathtest_parser.js
File metadata and controls
37 lines (33 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const fs = require('fs');
const jsonStr = fs.readFileSync('debug_gemini_full.json', 'utf8');
const response = JSON.parse(jsonStr);
if (response.error) {
console.log("API Error:", response.error);
process.exit(1);
}
const rawText = response.candidates[0].content.parts[0].text;
const parseGeminiJson = (raw) => {
const cleaned = raw
.replace(/```json/g, '')
.replace(/```/g, '')
.trim();
try {
return JSON.parse(cleaned);
} catch (_error) {
console.log("Basic parse failed.");
const start = cleaned.indexOf('{');
const end = cleaned.lastIndexOf('}');
if (start >= 0 && end > start) {
try {
return JSON.parse(cleaned.slice(start, end + 1));
} catch (err2) {
console.log("Fallback parse failed:", err2);
return null;
}
}
console.error("Gemini Parsing Complete Failure:", raw.substring(0, 200) + "...");
return null;
}
};
const result = parseGeminiJson(rawText);
console.log("Parsed result:", result ? "SUCCESS" : "FAIL");