|
| 1 | +import AsyncAlgorithms |
| 2 | +import BuiltinExtension |
| 3 | +import ChatBasic |
| 4 | +import Foundation |
| 5 | +import XcodeInspector |
| 6 | + |
| 7 | +#warning("This is a temporary implementation for proof of concept.") |
| 8 | + |
| 9 | +actor BuiltinExtensionChatCompletionsService { |
| 10 | + typealias RequestBody = ChatCompletionsRequestBody |
| 11 | + |
| 12 | + enum CustomError: Swift.Error, LocalizedError { |
| 13 | + case chatServiceNotFound |
| 14 | + |
| 15 | + var errorDescription: String? { |
| 16 | + switch self { |
| 17 | + case .chatServiceNotFound: |
| 18 | + return "Chat service not found." |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + var extensionManager: BuiltinExtensionManager { .shared } |
| 24 | + |
| 25 | + let extensionIdentifier: String |
| 26 | + let requestBody: RequestBody |
| 27 | + |
| 28 | + init(extensionIdentifier: String, requestBody: RequestBody) { |
| 29 | + self.extensionIdentifier = extensionIdentifier |
| 30 | + self.requestBody = requestBody |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +extension BuiltinExtensionChatCompletionsService: ChatCompletionsAPI { |
| 35 | + func callAsFunction() async throws -> ChatCompletionResponseBody { |
| 36 | + fatalError() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +extension BuiltinExtensionChatCompletionsService: ChatCompletionsStreamAPI { |
| 41 | + func callAsFunction( |
| 42 | + ) async throws -> AsyncThrowingStream<ChatCompletionsStreamDataChunk, Error> { |
| 43 | + let service = try getChatService() |
| 44 | + let (message, history) = extractMessageAndHistory(from: requestBody) |
| 45 | + guard let workspaceURL = XcodeInspector.shared.realtimeActiveWorkspaceURL, |
| 46 | + let projectURL = XcodeInspector.shared.realtimeActiveProjectURL |
| 47 | + else { throw CancellationError() } |
| 48 | + let stream = await service.sendMessage( |
| 49 | + message, |
| 50 | + history: history, |
| 51 | + references: [], |
| 52 | + workspace: .init( |
| 53 | + workspaceURL: workspaceURL, |
| 54 | + projectURL: projectURL |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + return stream.map { text in |
| 59 | + ChatCompletionsStreamDataChunk( |
| 60 | + id: nil, |
| 61 | + object: nil, |
| 62 | + model: nil, |
| 63 | + message: .init( |
| 64 | + role: .assistant, |
| 65 | + content: text, |
| 66 | + toolCalls: nil |
| 67 | + ), |
| 68 | + finishReason: nil |
| 69 | + ) |
| 70 | + }.toStream() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +extension BuiltinExtensionChatCompletionsService { |
| 75 | + func getChatService() throws -> any BuiltinExtensionChatServiceType { |
| 76 | + guard let ext = extensionManager.extensions |
| 77 | + .first(where: { $0.extensionIdentifier == extensionIdentifier }), |
| 78 | + let service = ext.chatService as? BuiltinExtensionChatServiceType |
| 79 | + else { |
| 80 | + throw CustomError.chatServiceNotFound |
| 81 | + } |
| 82 | + return service |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extension BuiltinExtensionChatCompletionsService { |
| 87 | + func extractMessageAndHistory( |
| 88 | + from request: RequestBody |
| 89 | + ) -> (message: String, history: [ChatMessage]) { |
| 90 | + let messages = request.messages |
| 91 | + |
| 92 | + if let lastIndexNotUserMessage = messages.lastIndex(where: { $0.role != .user }) { |
| 93 | + let message = messages[(lastIndexNotUserMessage + 1)...] |
| 94 | + .map { $0.content } |
| 95 | + .joined(separator: "\n\n") |
| 96 | + let history = Array(messages[0...lastIndexNotUserMessage]) |
| 97 | + return (message, history.map { |
| 98 | + .init(id: UUID().uuidString, role: $0.role.asChatMessageRole, content: $0.content) |
| 99 | + }) |
| 100 | + } else { // everything is user message |
| 101 | + let message = messages.map { $0.content }.joined(separator: "\n\n") |
| 102 | + return (message, []) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments