()
| 53 | } |
| 54 | |
| 55 | async refreshCopilotToken() { |
| 56 | console.log('🔄 Refreshing Copilot token...'); |
| 57 | |
| 58 | const options = { |
| 59 | hostname: 'api.github.com', |
| 60 | port: 443, |
| 61 | path: '/copilot_internal/v2/token', |
| 62 | method: 'GET', |
| 63 | headers: { |
| 64 | 'authorization': `token ${this.config.access_token}`, |
| 65 | 'user-agent': 'GithubCopilot/1.155.0' |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | try { |
| 70 | const response = await this.makeRequest(options); |
| 71 | |
| 72 | if (response.status === 200) { |
| 73 | this.copilotToken = response.data.token; |
| 74 | // Use the expires_at timestamp from the API response (subtract 60 seconds for safety margin) |
| 75 | this.tokenExpiry = (response.data.expires_at * 1000) - (60 * 1000); |
| 76 | console.log('✅ Copilot token refreshed successfully'); |
| 77 | console.log(`🕐 Token expires at: ${new Date(this.tokenExpiry).toISOString()}`); |
| 78 | return true; |
| 79 | } else { |
| 80 | console.error('❌ Error refreshing token:', response.data); |
| 81 | return false; |
| 82 | } |
| 83 | } catch (error) { |
| 84 | console.error('❌ Network error refreshing token:', error.message); |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | async ensureValidToken() { |
| 90 | if (!this.copilotToken || !this.tokenExpiry || Date.now() > this.tokenExpiry) { |
no test coverage detected