Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
214 lines (194 loc) · 6.43 KB
/
Copy pathbackground.js
File metadata and controls
214 lines (194 loc) · 6.43 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const DEEPSEEK_API_KEY = 'sk-d1e238a0001c49f7998d15beb638a30d';
async function analyzeIssue(issueData) {
try {
// Check if we have valid issue data
if (!issueData.title || !issueData.description ||
issueData.title.trim() === '' || issueData.description.trim() === '') {
return {
suggestions: [
"⚠️ Waiting for issue data to load...",
"Please make sure you're on a valid GitHub issue page.",
"The issue should have both a title and description."
].join('\n')
};
}
// Check if issue data is just the default values
if (issueData.title === "No title provided" &&
issueData.description === "No description provided") {
return {
suggestions: [
"⚠️ Issue data not found",
"Please make sure you're on a valid GitHub issue page.",
"Waiting for issue content to load..."
].join('\n')
};
}
const requestBody = {
model: "deepseek-chat",
messages: [
{
role: "system",
content: `You are an expert programmer analyzing GitHub issues.
Analyze the code issues and provide solutions in a structured format.
Return your response as a JSON object with specific sections.`
},
{
role: "user",
content: `Analyze this GitHub issue and provide a solution in JSON format:
Repository: ${issueData.repoName}
Primary Language: ${issueData.primaryLanguage}
Issue Title: ${issueData.title}
Issue Description:
${issueData.description}`
}
],
temperature: 0.3,
max_tokens: 2000,
stream: false,
top_p: 0.95,
presence_penalty: 0,
frequency_penalty: 0,
response_format: {
type: "json_object",
schema: {
type: "object",
properties: {
analysis: {
type: "string",
description: "Brief analysis of the issue"
},
solution: {
type: "array",
items: {
type: "string"
},
description: "List of specific solution steps"
},
code_changes: {
type: "array",
items: {
type: "string"
},
description: "Suggested code modifications"
},
testing: {
type: "array",
items: {
type: "string"
},
description: "Testing suggestions"
}
}
}
}
};
console.log('Issue data received:', {
title: issueData.title,
description: issueData.description.substring(0, 100) + '...',
repo: issueData.repoName,
language: issueData.primaryLanguage
});
// Only proceed with API call if we have valid data
console.log('Request body:', JSON.stringify(requestBody, null, 2));
const response = await fetch('https://api.deepseek.com/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${DEEPSEEK_API_KEY}`
},
body: JSON.stringify(requestBody)
});
const responseData = await response.json();
console.log('API Response:', responseData);
if (!response.ok) {
throw new Error(`API Error: ${responseData.error?.message || JSON.stringify(responseData)}`);
}
return { suggestions: responseData.choices[0].message.content };
} catch (error) {
console.error('Error analyzing issue:', error);
throw error;
}
}
// Add a function to test API connection
async function testApiConnection() {
try {
const response = await fetch('https://api.deepseek.com/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${DEEPSEEK_API_KEY}`
},
body: JSON.stringify({
model: "deepseek-chat",
messages: [
{
role: "user",
content: "Hello"
}
],
max_tokens: 5
})
});
if (!response.ok) {
throw new Error('API connection test failed');
}
return true;
} catch (error) {
console.error('API connection test failed:', error);
return false;
}
}
// Modified message listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'analyzeIssue') {
console.log('Background script received analyzeIssue request:', request.data);
analyzeIssue(request.data)
.then(response => {
console.log('Analysis complete, sending response:', response);
if (!response.error) {
try {
// Only try to parse as JSON if it's an API response
if (response.suggestions.startsWith('⚠️')) {
sendResponse({ suggestions: response.suggestions });
return;
}
// Parse JSON response
const parsedSuggestions = JSON.parse(response.suggestions);
// Format the response for display
const formattedResponse = [
'📊 Analysis:',
parsedSuggestions.analysis,
'',
'🛠️ Solution Steps:',
...parsedSuggestions.solution.map(s => `• ${s}`),
'',
'💻 Code Changes:',
...parsedSuggestions.code_changes.map(c => `• ${c}`),
'',
'🧪 Testing:',
...parsedSuggestions.testing.map(t => `• ${t}`)
].join('\n');
sendResponse({ suggestions: formattedResponse });
} catch (error) {
console.error('Error parsing response:', error);
// Fallback to simple formatting if JSON parsing fails
sendResponse({ suggestions: response.suggestions });
}
} else {
console.error('Error in response:', response.error);
sendResponse({ error: response.error });
}
})
.catch(error => {
console.error('Error in analyzeIssue:', error);
sendResponse({ error: error.message });
});
return true;
}
if (request.action === 'testConnection') {
testApiConnection()
.then(success => sendResponse({ success }))
.catch(error => sendResponse({ error: error.message }));
return true;
}
});