Enhance runLLMCommitRewrite logic in main.js
This commit is contained in:
63
main.js
63
main.js
@@ -1221,27 +1221,62 @@ module.exports = { rewordCommitsSequentially };
|
|||||||
|
|
||||||
//---- 6. Workflow ----
|
//---- 6. Workflow ----
|
||||||
async function runLLMCommitRewrite(folderObj, win) {
|
async function runLLMCommitRewrite(folderObj, win) {
|
||||||
if(!folderObj.needsRelocation){
|
if(folderObj.needsRelocation) return;
|
||||||
const hashes = folderObj.llmCandidates;
|
|
||||||
const birthday = folderObj.firstCandidateBirthday;
|
// Refresh the latest folder state from store to avoid stale references.
|
||||||
const folderPath = folderObj.path;
|
let folders = store.get('folders') || [];
|
||||||
folderObj.llmCandidates = [];
|
let idx = folders.findIndex(f => f.path === folderObj.path);
|
||||||
folderObj.firstCandidateBirthday = null;
|
if (idx === -1) return;
|
||||||
folderObj.linesChanged = 0;
|
const folderPath = folders[idx].path;
|
||||||
const folders = store.get('folders') || [];
|
|
||||||
const idx = folders.findIndex(f => f.path === folderObj.path);
|
const hashes = (folders[idx].llmCandidates || []).slice();
|
||||||
if (idx !== -1) {
|
if (!hashes.length) return;
|
||||||
folders[idx] = folderObj;
|
if (folders[idx].rewriteInProgress) {
|
||||||
store.set('folders', folders);
|
debug(`[runLLMCommitRewrite] Rewrite already in progress for ${folderPath}, skipping.`);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const originalBirthday = folders[idx].firstCandidateBirthday;
|
||||||
|
|
||||||
|
// Lock: mark rewrite in progress
|
||||||
|
folders[idx].rewriteInProgress = true;
|
||||||
|
store.set('folders', folders);
|
||||||
|
|
||||||
|
let error;
|
||||||
|
try {
|
||||||
const prompt = await generateLLMCommitMessages(folderPath, hashes);
|
const prompt = await generateLLMCommitMessages(folderPath, hashes);
|
||||||
const llmRaw = await streamLLMCommitMessages(prompt, chunk => process.stdout.write(chunk), win);
|
const llmRaw = await streamLLMCommitMessages(prompt, chunk => process.stdout.write(chunk), win);
|
||||||
const commitList = parseLLMCommitMessages(llmRaw);
|
const commitList = parseLLMCommitMessages(llmRaw);
|
||||||
const messageMap = {};
|
const messageMap = {};
|
||||||
for (const entry of commitList) messageMap[entry.commit] = entry.newMessage;
|
for (const entry of commitList) messageMap[entry.commit] = entry.newMessage;
|
||||||
await rewordCommitsSequentially(folderPath, messageMap, hashes);
|
await rewordCommitsSequentially(folderPath, messageMap, hashes);
|
||||||
win.webContents.send('repo-updated', folderObj.path);
|
win.webContents.send('repo-updated', folderPath);
|
||||||
|
} catch (err) {
|
||||||
|
error = err;
|
||||||
|
console.error('[runLLMCommitRewrite] Rewrite failed:', err);
|
||||||
|
} finally {
|
||||||
|
// Refresh state in case it changed during the rewrite
|
||||||
|
folders = store.get('folders') || [];
|
||||||
|
idx = folders.findIndex(f => f.path === folderPath);
|
||||||
|
if (idx !== -1) {
|
||||||
|
const buffer = folders[idx].llmBuffer || [];
|
||||||
|
if (error) {
|
||||||
|
// Requeue original candidates plus anything that arrived during rewrite
|
||||||
|
folders[idx].llmCandidates = hashes.concat(buffer);
|
||||||
|
folders[idx].firstCandidateBirthday = originalBirthday || (folders[idx].llmCandidates.length ? Date.now() : null);
|
||||||
|
} else {
|
||||||
|
// Promote buffered commits to become the next batch
|
||||||
|
folders[idx].llmCandidates = buffer;
|
||||||
|
folders[idx].firstCandidateBirthday = buffer.length ? Date.now() : null;
|
||||||
|
folders[idx].linesChanged = 0;
|
||||||
|
}
|
||||||
|
folders[idx].llmBuffer = [];
|
||||||
|
folders[idx].rewriteInProgress = false;
|
||||||
|
store.set('folders', folders);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user