|
| 1 | +import AIModel |
| 2 | +import AsyncAlgorithms |
| 3 | +import ChatBasic |
| 4 | +import Foundation |
| 5 | +import GitHubCopilotService |
| 6 | +import Logger |
| 7 | +import Preferences |
| 8 | + |
| 9 | +/// Looks like it's used in many other popular repositories so maybe it's safe. |
| 10 | +actor GitHubCopilotEmbeddingService: EmbeddingAPI { |
| 11 | + let chatModel: EmbeddingModel |
| 12 | + |
| 13 | + init(model: EmbeddingModel) { |
| 14 | + var model = model |
| 15 | + model.format = .openAICompatible |
| 16 | + chatModel = model |
| 17 | + } |
| 18 | + |
| 19 | + func embed(text: String) async throws -> EmbeddingResponse { |
| 20 | + let service = try await buildService() |
| 21 | + return try await service.embed(text: text) |
| 22 | + } |
| 23 | + |
| 24 | + func embed(texts: [String]) async throws -> EmbeddingResponse { |
| 25 | + let service = try await buildService() |
| 26 | + return try await service.embed(texts: texts) |
| 27 | + } |
| 28 | + |
| 29 | + func embed(tokens: [[Int]]) async throws -> EmbeddingResponse { |
| 30 | + let service = try await buildService() |
| 31 | + return try await service.embed(tokens: tokens) |
| 32 | + } |
| 33 | + |
| 34 | + private func buildService() async throws -> OpenAIEmbeddingService { |
| 35 | + let token = try await GitHubCopilotExtension.fetchToken() |
| 36 | + |
| 37 | + return OpenAIEmbeddingService( |
| 38 | + apiKey: token.token, |
| 39 | + model: chatModel, |
| 40 | + endpoint: token.endpoints.api + "/embeddings" |
| 41 | + ) { request in |
| 42 | + |
| 43 | +// POST /chat/completions HTTP/2 |
| 44 | +// :authority: api.individual.githubcopilot.com |
| 45 | +// authorization: Bearer * |
| 46 | +// x-request-id: * |
| 47 | +// openai-organization: github-copilot |
| 48 | +// vscode-sessionid: * |
| 49 | +// vscode-machineid: * |
| 50 | +// editor-version: vscode/1.89.1 |
| 51 | +// editor-plugin-version: Copilot for Xcode/0.35.5 |
| 52 | +// copilot-language-server-version: 1.236.0 |
| 53 | +// x-github-api-version: 2023-07-07 |
| 54 | +// openai-intent: conversation-panel |
| 55 | +// content-type: application/json |
| 56 | +// user-agent: GithubCopilot/1.236.0 |
| 57 | +// content-length: 9061 |
| 58 | +// accept: */* |
| 59 | +// accept-encoding: gzip,deflate,br |
| 60 | + |
| 61 | + request.setValue( |
| 62 | + "Copilot for Xcode/\(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown")", |
| 63 | + forHTTPHeaderField: "Editor-Version" |
| 64 | + ) |
| 65 | + request.setValue("Bearer \(token.token)", forHTTPHeaderField: "Authorization") |
| 66 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 67 | + request.setValue("vscode-chat", forHTTPHeaderField: "Copilot-Integration-Id") |
| 68 | + request.setValue("2023-07-07", forHTTPHeaderField: "X-Github-Api-Version") |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
0 commit comments