* Make an authenticated request to the client's Copilot endpoint with its * token. Used for Copilot API requests.
(
path: string,
message: string
)
| 1857 | * token. Used for Copilot API requests. |
| 1858 | */ |
| 1859 | private async copilotRequest( |
| 1860 | path: string, |
| 1861 | message: string |
| 1862 | ): Promise<CopilotChatCompletionResponse> { |
| 1863 | if (!this.copilotEndpoint) { |
| 1864 | throw new Error('No Copilot endpoint available') |
| 1865 | } |
| 1866 | |
| 1867 | const response = await this.request(this.copilotEndpoint, 'POST', path, { |
| 1868 | body: { |
| 1869 | messages: [ |
| 1870 | { |
| 1871 | role: 'user', |
| 1872 | content: message, |
| 1873 | }, |
| 1874 | ], |
| 1875 | stream: false, |
| 1876 | response_format: { |
| 1877 | type: 'json_object', |
| 1878 | }, |
| 1879 | }, |
| 1880 | customHeaders: { |
| 1881 | 'X-Initiator': 'user', |
| 1882 | 'X-Interaction-ID': crypto.randomUUID(), |
| 1883 | 'X-Interaction-Type': 'generateCommitMessage', |
| 1884 | }, |
| 1885 | }) |
| 1886 | |
| 1887 | if (response.status === HttpStatusCode.TooManyRequests) { |
| 1888 | const retryAfter = response.headers.get('Retry-After') |
| 1889 | if (retryAfter) { |
| 1890 | throw new CopilotError( |
| 1891 | `Rate limited, retry after ${retryAfter} seconds.`, |
| 1892 | response.status |
| 1893 | ) |
| 1894 | } else { |
| 1895 | throw new CopilotError( |
| 1896 | 'Rate limited, try again in a few minutes.', |
| 1897 | response.status |
| 1898 | ) |
| 1899 | } |
| 1900 | } else if (response.status === HttpStatusCode.PaymentRequired) { |
| 1901 | throw parseCopilotPaymentRequiredError( |
| 1902 | await response.text(), |
| 1903 | response.headers.get('Retry-After') |
| 1904 | ) |
| 1905 | } else if (response.status === HttpStatusCode.Unauthorized) { |
| 1906 | throw new CopilotError( |
| 1907 | 'Unauthorized: error with authentication.', |
| 1908 | response.status |
| 1909 | ) |
| 1910 | } else if (response.status === HttpStatusCode.Forbidden) { |
| 1911 | const body = await response.text() |
| 1912 | if (body.includes('unauthorized: not licensed to use Copilot')) { |
| 1913 | throw new CopilotError( |
| 1914 | 'Unauthorized: not licensed to use Copilot.', |
| 1915 | response.status |
| 1916 | ) |
no test coverage detected