| 23 | } |
| 24 | |
| 25 | async refreshCopilotToken() { |
| 26 | console.log('🔄 Refreshing Copilot token...'); |
| 27 | |
| 28 | return new Promise((resolve, reject) => { |
| 29 | const options = { |
| 30 | hostname: 'api.github.com', |
| 31 | port: 443, |
| 32 | path: '/copilot_internal/v2/token', |
| 33 | method: 'GET', |
| 34 | headers: { |
| 35 | 'authorization': `token ${this.config.access_token}`, |
| 36 | 'user-agent': 'GithubCopilot/1.155.0' |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | const req = https.request(options, (res) => { |
| 41 | let body = ''; |
| 42 | res.on('data', chunk => body += chunk); |
| 43 | res.on('end', () => { |
| 44 | try { |
| 45 | if (res.statusCode === 200) { |
| 46 | const response = JSON.parse(body); |
| 47 | this.copilotToken = response.token; |
| 48 | this.tokenExpiry = (response.expires_at * 1000) - (60 * 1000); |
| 49 | console.log('✅ Copilot token refreshed successfully'); |
| 50 | console.log(`🕐 Token expires at: ${new Date(this.tokenExpiry).toISOString()}`); |
| 51 | resolve(true); |
| 52 | } else { |
| 53 | console.error('❌ Error refreshing token:', body); |
| 54 | resolve(false); |
| 55 | } |
| 56 | } catch (error) { |
| 57 | console.error('❌ Parse error:', error.message); |
| 58 | resolve(false); |
| 59 | } |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | req.on('error', (error) => { |
| 64 | console.error('❌ Network error refreshing token:', error.message); |
| 65 | resolve(false); |
| 66 | }); |
| 67 | |
| 68 | req.end(); |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | async getValidToken() { |
| 73 | if (!this.copilotToken || !this.tokenExpiry || Date.now() > this.tokenExpiry) { |