Skip to content

Commit 8f22fc1

Browse files
committed
fix: Improve JSON extraction from AI response and update getConfig function for clarity
1 parent 6257ed1 commit 8f22fc1

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/commands/other/translate.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,13 @@ ${html}
135135
try {
136136
const result = await model.generateContent(prompt);
137137
let jsonText = result.response.candidates[0].content.parts[0].text;
138-
jsonText = jsonText
139-
.replace(/^```json\s*([\s\S]*?)\s*```$/i, "$1")
140-
.trim();
138+
// Extract the first JSON object from the response
139+
const match = jsonText.match(/\{[\s\S]*\}/);
140+
if (!match) throw new Error("No JSON object found in AI response");
141+
jsonText = match[0];
141142
return JSON.parse(jsonText).translations;
142143
} catch (err) {
143-
console.error(`AI error:`, err);
144+
console.log(`AI error:`, err);
144145
return null;
145146
}
146147
}

src/getConfig.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
const fs = require("fs");
22
const path = require("path");
33

4-
async function getConfig(directory, filename = "CoCreate.config.js") {
5-
let config;
6-
let configPathname = path.resolve(directory, filename);
7-
let configPath = configPathname;
8-
if (!configPath.includes("node_modules/")) {
9-
configPath = findClosestConfig(configPath, "CoCreate.config.js");
10-
if (configPath) {
11-
config = require(configPath);
12-
config.configPath = configPath;
13-
config.filePath = configPathname;
14-
} else {
15-
console.log("No CoCreate.config file found in parent directories.");
16-
}
4+
async function getConfig(directory, filename = "") {
5+
let configPath = findClosestConfig(directory, "CoCreate.config.js");
6+
if (configPath) {
7+
let config = require(configPath);
8+
config.configPath = configPath;
9+
config.filePath = path.resolve(directory, filename);
10+
return config;
11+
} else {
12+
console.log("No CoCreate.config file found in parent directories.");
1713
}
18-
19-
return config;
2014
}
2115

22-
function findClosestConfig(filePath, filename) {
23-
let currentDir = filePath;
16+
function findClosestConfig(directory, filename) {
17+
let currentDir = directory;
2418

2519
while (currentDir !== "/" && currentDir !== ".") {
2620
let configFile = path.join(currentDir, filename);

0 commit comments

Comments
 (0)