|
| 1 | +import AIModel |
| 2 | +import AsyncAlgorithms |
| 3 | +import ChatBasic |
| 4 | +import Foundation |
| 5 | +import GitHubCopilotService |
| 6 | +import Logger |
| 7 | +import Preferences |
| 8 | + |
| 9 | +public enum AvailableGitHubCopilotModel: String, CaseIterable { |
| 10 | + case claude35sonnet = "claude-3.5-sonnet" |
| 11 | + case o1Mini = "o1-mini" |
| 12 | + case o1 = "o1" |
| 13 | + case gpt4Turbo = "gpt-4-turbo" |
| 14 | + case gpt4oMini = "gpt-4o-mini" |
| 15 | + case gpt4o = "gpt-4o" |
| 16 | + case gpt4 = "gpt-4" |
| 17 | + case gpt35Turbo = "gpt-3.5-turbo" |
| 18 | + |
| 19 | + public var contextWindow: Int { |
| 20 | + switch self { |
| 21 | + case .claude35sonnet: |
| 22 | + return 200_000 |
| 23 | + case .o1Mini: |
| 24 | + return 128_000 |
| 25 | + case .o1: |
| 26 | + return 128_000 |
| 27 | + case .gpt4Turbo: |
| 28 | + return 128_000 |
| 29 | + case .gpt4oMini: |
| 30 | + return 128_000 |
| 31 | + case .gpt4o: |
| 32 | + return 128_000 |
| 33 | + case .gpt4: |
| 34 | + return 32_768 |
| 35 | + case .gpt35Turbo: |
| 36 | + return 16_384 |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Looks like it's used in many other popular repositories so maybe it's safe. |
| 42 | +actor GitHubCopilotChatCompletionsService: ChatCompletionsStreamAPI, ChatCompletionsAPI { |
| 43 | + |
| 44 | + let chatModel: ChatModel |
| 45 | + let requestBody: ChatCompletionsRequestBody |
| 46 | + |
| 47 | + init( |
| 48 | + model: ChatModel, |
| 49 | + requestBody: ChatCompletionsRequestBody |
| 50 | + ) { |
| 51 | + var model = model |
| 52 | + model.format = .openAICompatible |
| 53 | + chatModel = model |
| 54 | + self.requestBody = requestBody |
| 55 | + } |
| 56 | + |
| 57 | + func callAsFunction() async throws |
| 58 | + -> AsyncThrowingStream<ChatCompletionsStreamDataChunk, any Error> |
| 59 | + { |
| 60 | + let service = try await buildService() |
| 61 | + return try await service() |
| 62 | + } |
| 63 | + |
| 64 | + func callAsFunction() async throws -> ChatCompletionResponseBody { |
| 65 | + let service = try await buildService() |
| 66 | + return try await service() |
| 67 | + } |
| 68 | + |
| 69 | + private func buildService() async throws -> OpenAIChatCompletionsService { |
| 70 | + let token = try await GitHubCopilotExtension.fetchToken() |
| 71 | + |
| 72 | + guard let endpoint = URL(string: token.endpoints.api + "/chat/completions") else { |
| 73 | + throw ChatGPTServiceError.endpointIncorrect |
| 74 | + } |
| 75 | + |
| 76 | + return OpenAIChatCompletionsService( |
| 77 | + apiKey: token.token, |
| 78 | + model: chatModel, |
| 79 | + endpoint: endpoint, |
| 80 | + requestBody: requestBody |
| 81 | + ) { request in |
| 82 | + |
| 83 | +// POST /chat/completions HTTP/2 |
| 84 | +// :authority: api.individual.githubcopilot.com |
| 85 | +// authorization: Bearer * |
| 86 | +// x-request-id: * |
| 87 | +// openai-organization: github-copilot |
| 88 | +// vscode-sessionid: * |
| 89 | +// vscode-machineid: * |
| 90 | +// editor-version: vscode/1.89.1 |
| 91 | +// editor-plugin-version: Copilot for Xcode/0.35.5 |
| 92 | +// copilot-language-server-version: 1.236.0 |
| 93 | +// x-github-api-version: 2023-07-07 |
| 94 | +// openai-intent: conversation-panel |
| 95 | +// content-type: application/json |
| 96 | +// user-agent: GithubCopilot/1.236.0 |
| 97 | +// content-length: 9061 |
| 98 | +// accept: */* |
| 99 | +// accept-encoding: gzip,deflate,br |
| 100 | + |
| 101 | + request.setValue( |
| 102 | + "Copilot for Xcode/\(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown")", |
| 103 | + forHTTPHeaderField: "Editor-Version" |
| 104 | + ) |
| 105 | + request.setValue("Bearer \(token.token)", forHTTPHeaderField: "Authorization") |
| 106 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 107 | + request.setValue("vscode-chat", forHTTPHeaderField: "Copilot-Integration-Id") |
| 108 | + request.setValue("2023-07-07", forHTTPHeaderField: "X-Github-Api-Version") |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
0 commit comments