Skip to content

Commit feee241

Browse files
Copilotwarengonzaga
andcommitted
🔧 update: address PR review feedback on code quality
Co-authored-by: warengonzaga <15052701+warengonzaga@users.noreply.github.com>
1 parent 5eae5a9 commit feee241

5 files changed

Lines changed: 48 additions & 29 deletions

File tree

source/cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from './commands/auth.js';
1313
import {showConfig, resetConfig} from './commands/config.js';
1414
import {setConvention} from './utils/config-manager.js';
15+
import {CONVENTIONS} from './utils/commit-conventions.js';
1516

1617
const program = new Command();
1718

@@ -92,7 +93,7 @@ configCmd
9293
.command('set-convention <type>')
9394
.description('Set default commit convention')
9495
.action(type => {
95-
const validConventions = ['clean', 'conventional', 'gitmoji', 'simple'];
96+
const validConventions = Object.keys(CONVENTIONS);
9697
if (!validConventions.includes(type)) {
9798
showError(`Invalid convention: ${type}`);
9899
console.log(`Available: ${validConventions.join(', ')}`);

source/commands/commit.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ export async function processFilesInteractively(aiProvider, options = {}) {
139139

140140
let currentConvention = options.convention || getConvention();
141141
let fileProcessed = false;
142+
let attempts = 0;
143+
const maxAttempts = 5;
142144

143-
while (!fileProcessed) {
145+
while (!fileProcessed && attempts < maxAttempts) {
146+
attempts++;
144147
try {
145148
const message = await aiProvider.generateCommitMessage(diff, file, {
146149
...options,
@@ -188,6 +191,15 @@ export async function processFilesInteractively(aiProvider, options = {}) {
188191
fileProcessed = true;
189192
}
190193
}
194+
195+
// Check if max attempts reached without processing
196+
if (!fileProcessed && attempts >= maxAttempts) {
197+
showWarning(
198+
`Maximum regeneration attempts (${maxAttempts}) reached for ${file}`,
199+
);
200+
await unstageFile(file);
201+
skipped++;
202+
}
191203
}
192204

193205
// Show summary

source/providers/ai-provider.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ export class AIProvider {
6464
*/
6565
async generateWithCopilot(prompt, options = {}) {
6666
let client;
67+
let clientStarted = false;
68+
6769
try {
6870
// Create and start the Copilot client
6971
client = new CopilotClient();
7072
await client.start();
73+
clientStarted = true;
7174

7275
// Create session with model
7376
const session = await client.createSession({
@@ -79,25 +82,30 @@ export class AIProvider {
7982
prompt,
8083
});
8184

82-
// Clean up
83-
await client.stop();
85+
// Extract the content from response, handling multiple possible shapes
86+
let content = null;
87+
88+
if (typeof response === 'string') {
89+
content = response;
90+
} else {
91+
content =
92+
// Preserve original expected shape first
93+
response?.data?.content ??
94+
// Common OpenAI / chat-like shapes under data
95+
response?.data?.choices?.[0]?.message?.content ??
96+
response?.data?.choices?.[0]?.content ??
97+
// Or directly on the response object
98+
response?.choices?.[0]?.message?.content ??
99+
response?.choices?.[0]?.content ??
100+
response?.content;
101+
}
84102

85-
// Extract the content from response
86-
if (response?.data?.content) {
87-
return response.data.content.trim();
103+
if (typeof content === 'string' && content.trim()) {
104+
return content.trim();
88105
}
89106

90107
throw new Error('No response from Copilot');
91108
} catch (error) {
92-
// Clean up client if it was created
93-
if (client) {
94-
try {
95-
await client.stop();
96-
} catch {
97-
// Ignore cleanup errors
98-
}
99-
}
100-
101109
console.error('Copilot error:', error.message);
102110

103111
// Fallback to OpenAI if available
@@ -114,6 +122,15 @@ export class AIProvider {
114122
'3. GitHub CLI installed and in PATH\n\n' +
115123
'Or set OpenAI key as fallback: magicc auth openai <key>',
116124
);
125+
} finally {
126+
// Clean up client if it was started
127+
if (client && clientStarted) {
128+
try {
129+
await client.stop();
130+
} catch {
131+
// Ignore cleanup errors
132+
}
133+
}
117134
}
118135
}
119136

source/utils/config-manager.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,13 @@ export function getAuthMode() {
1818

1919
// Token management
2020
export function setToken(provider, token) {
21-
if (provider === 'copilot' || provider === 'github') {
22-
config.set('githubToken', token);
23-
} else if (provider === 'openai') {
21+
if (provider === 'openai') {
2422
config.set('openai', token);
23+
config.set('authenticatedAt', new Date().toISOString());
2524
}
26-
27-
config.set('authenticatedAt', new Date().toISOString());
2825
}
2926

3027
export function getToken(provider) {
31-
if (provider === 'copilot' || provider === 'github') {
32-
return config.get('githubToken');
33-
}
34-
3528
if (provider === 'openai') {
3629
return config.get('openai');
3730
}

test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ test('config manager stores and retrieves tokens', t => {
3131
setToken('openai', 'sk-test-key');
3232
t.is(getToken('openai'), 'sk-test-key');
3333

34-
setToken('github', 'ghp-test-token');
35-
t.is(getToken('github'), 'ghp-test-token');
36-
3734
clearAll();
3835
t.is(getToken('openai'), undefined);
39-
t.is(getToken('github'), undefined);
4036
});
4137

4238
test('config manager stores and retrieves convention', t => {

0 commit comments

Comments
 (0)