1
0

auto-git:

[change] main.js
This commit is contained in:
2025-05-24 14:48:14 +02:00
parent ca82c5ad1f
commit 92d0de8205

85
main.js
View File

@@ -214,7 +214,8 @@ Generate a concise git commit message for these changes:
${diff} ${diff}
Don't give any feedback on the code, just analyze what changed and write the git commit message. Keep it short! A commit message MUST NOT exceed 70 characters!`; Don't give any feedback on the code, just analyze what changed and write the git commit message. Keep it short! A commit message MUST NOT BE STRAIGHT TO THE POINT!
Also reply to my message, just give me the commit message.`;
} else if (commits.length > 1) { } else if (commits.length > 1) {
// Multiple commits: Squash them, give all diffs as a big change. // Multiple commits: Squash them, give all diffs as a big change.
const combinedDiffs = commits.map(c => c.diff).join('\n\n'); const combinedDiffs = commits.map(c => c.diff).join('\n\n');
@@ -226,52 +227,60 @@ Here are the combined diffs:
-------------------------------------- --------------------------------------
Even if this might seem like a lot of code, I need you to answer in a SINGLE SENTENCE. A git commit message to be precise is what I need from you, to protocol these changes. Even if this might seem like a lot of code, I need you to answer in a SINGLE SENTENCE. A git commit message to be precise is what I need from you, to protocol these changes.
Don't give any feedback on the code! Just analyze what changed and write the git commit message. Keep it short! A commit message MUST NOT exceed 70 characters!`; Don't give any feedback on the code! Just analyze what changed and write the git commit message. Keep it short! A commit message MUST NOT BE STRAIGHT TO THE POINT!
Don't reply to my message, just give me the commit message.`;
} else { } else {
throw new Error('No commits found for LLM prompt.'); throw new Error('No commits found for LLM prompt.');
} }
} }
// 3. LLM Streaming Call // 3. LLM Streaming Call
async function streamLLMCommitMessages(prompt, onDataChunk) { async function streamLLMCommitMessages(prompt, onDataChunk, maxLen = 500) {
console.log(prompt); return new Promise((resolve, reject) => {
const response = await fetch('http://localhost:11434/api/generate', { const child = spawn('ollama', [
method: 'POST', 'chat', '--stream',
headers: { 'Content-Type': 'application/json' }, '--model', 'qwen2.5-coder:32b',
body: JSON.stringify({ '--prompt', prompt
model: 'qwen2.5-coder:32b', ]);
prompt: prompt,
stream: true
})
});
if (!response.body) throw new Error('No stream returned'); let out = '';
const reader = response.body.getReader(); let resolved = false;
const decoder = new TextDecoder();
let fullOutput = ''; child.stdout.on('data', chunk => {
let done = false; const s = chunk.toString();
while (!done) { out += s;
const { value, done: streamDone } = await reader.read(); if (onDataChunk) onDataChunk(s);
done = streamDone;
if (value) { // Cut at first linebreak OR at maxLen
const chunk = decoder.decode(value, { stream: true }); const firstLine = out.split('\n')[0];
for (const line of chunk.split('\n')) { if (!resolved && (firstLine.length >= maxLen || out.includes('\n'))) {
if (!line.trim()) continue; resolved = true;
try { child.kill();
const obj = JSON.parse(line); resolve(firstLine.slice(0, maxLen).trim());
if (obj.response) {
fullOutput += obj.response;
if (onDataChunk) onDataChunk(obj.response);
}
if (obj.done) break;
} catch (e) {
// ignore malformed chunk
}
} }
} });
}
return fullOutput; child.stderr.on('data', chunk => {
if (!resolved) {
resolved = true;
reject(chunk.toString());
}
});
child.on('close', () => {
if (!resolved) {
const firstLine = out.split('\n')[0];
resolve(firstLine.slice(0, maxLen).trim());
}
});
child.on('error', err => {
if (!resolved) {
resolved = true;
reject(err);
}
});
});
} }
async function squashCommitMessages(repoPath, commitMessage, hashes) { async function squashCommitMessages(repoPath, commitMessage, hashes) {