-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
67 lines (58 loc) · 1.88 KB
/
Copy pathbackground.js
File metadata and controls
67 lines (58 loc) · 1.88 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
const config = {
provider: 'openai', // or "azure"
apiKey: 'YOUR_API_KEY_HERE',
endpoint: '', // required for Azure
model: 'gpt-3.5-turbo',
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'generateTitle') {
;(async () => {
const messages = message.data
const payload = {
messages: [
{
role: 'system',
content:
'You are an assistant that generates clear, descriptive pull request titles based on commit messages.',
},
{
role: 'user',
content: `Generate a concise and descriptive pull request title based on the following commit messages:\n\n${messages.join(
'\n',
)}`,
},
],
temperature: 0.7,
}
try {
let url = ''
let headers = {
'Content-Type': 'application/json',
}
if (config.provider === 'openai') {
url = 'https://api.openai.com/v1/chat/completions'
headers['Authorization'] = `Bearer ${config.apiKey}`
payload.model = config.model
} else if (config.provider === 'azure') {
url = `${config.endpoint}/openai/deployments/${config.model}/chat/completions?api-version=2023-07-01-preview`
headers['api-key'] = config.apiKey
}
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(payload),
})
const result = await response.json()
let title = 'Untitled PR'
if (result.choices?.[0]?.message?.content) {
title = result.choices[0].message.content.trim()
}
sendResponse({ title })
} catch (err) {
console.error('AI API error:', err)
sendResponse({ title: null })
}
})()
return true // Indicates async response
}
})