|
1 | 1 | import ChatService |
| 2 | +import CopilotModel |
| 3 | +import CopilotService |
2 | 4 | import Foundation |
3 | 5 | import OpenAIService |
| 6 | +import PromptToCodeService |
4 | 7 | import SuggestionWidget |
5 | 8 |
|
| 9 | +@ServiceActor |
6 | 10 | final class WidgetDataSource { |
7 | 11 | static let shared = WidgetDataSource() |
8 | 12 |
|
9 | | - var globalChat: ChatService? |
10 | | - var chats = [URL: ChatService]() |
11 | | - var globalChatProvider: ChatProvider? |
12 | | - var chatProviders = [URL: ChatProvider]() |
| 13 | + final class Chat { |
| 14 | + let chatService: ChatService |
| 15 | + let provider: ChatProvider |
| 16 | + public init(chatService: ChatService, provider: ChatProvider) { |
| 17 | + self.chatService = chatService |
| 18 | + self.provider = provider |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + final class PromptToCode { |
| 23 | + let promptToCodeService: PromptToCodeService |
| 24 | + let provider: PromptToCodeProvider |
| 25 | + public init( |
| 26 | + promptToCodeService: PromptToCodeService, |
| 27 | + provider: PromptToCodeProvider |
| 28 | + ) { |
| 29 | + self.promptToCodeService = promptToCodeService |
| 30 | + self.provider = provider |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private(set) var globalChat: Chat? |
| 35 | + private(set) var chats = [URL: Chat]() |
| 36 | + private(set) var promptToCodes = [URL: PromptToCode]() |
13 | 37 |
|
14 | 38 | private init() {} |
15 | 39 |
|
16 | 40 | @discardableResult |
17 | 41 | func createChatIfNeeded(for url: URL) -> ChatService { |
| 42 | + let build = { |
| 43 | + let service = ChatService(chatGPTService: ChatGPTService()) |
| 44 | + let provider = ChatProvider( |
| 45 | + service: service, |
| 46 | + fileURL: url, |
| 47 | + onCloseChat: { [weak self] in |
| 48 | + if UserDefaults.shared.value(for: \.useGlobalChat) { |
| 49 | + self?.globalChat = nil |
| 50 | + } else { |
| 51 | + self?.removeChat(for: url) |
| 52 | + } |
| 53 | + let presenter = PresentInWindowSuggestionPresenter() |
| 54 | + presenter.closeChatRoom(fileURL: url) |
| 55 | + }, |
| 56 | + onSwitchContext: { [weak self] in |
| 57 | + let useGlobalChat = UserDefaults.shared.value(for: \.useGlobalChat) |
| 58 | + UserDefaults.shared.set(!useGlobalChat, for: \.useGlobalChat) |
| 59 | + self?.createChatIfNeeded(for: url) |
| 60 | + let presenter = PresentInWindowSuggestionPresenter() |
| 61 | + presenter.presentChatRoom(fileURL: url) |
| 62 | + } |
| 63 | + ) |
| 64 | + return Chat(chatService: service, provider: provider) |
| 65 | + } |
| 66 | + |
18 | 67 | let useGlobalChat = UserDefaults.shared.value(for: \.useGlobalChat) |
19 | | - let chat: ChatService |
20 | | - |
21 | 68 | if useGlobalChat { |
22 | | - chat = globalChat ?? ChatService(chatGPTService: ChatGPTService()) |
23 | | - globalChat = chat |
| 69 | + if let globalChat { |
| 70 | + return globalChat.chatService |
| 71 | + } |
| 72 | + let newChat = build() |
| 73 | + globalChat = newChat |
| 74 | + return newChat.chatService |
24 | 75 | } else { |
25 | | - chat = chats[url] ?? ChatService(chatGPTService: ChatGPTService()) |
26 | | - chats[url] = chat |
| 76 | + if let chat = chats[url] { |
| 77 | + return chat.chatService |
| 78 | + } |
| 79 | + let newChat = build() |
| 80 | + chats[url] = newChat |
| 81 | + return newChat.chatService |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @discardableResult |
| 86 | + func createPromptToCode( |
| 87 | + for url: URL, |
| 88 | + code: String, |
| 89 | + selectionRange: CursorRange, |
| 90 | + language: CopilotLanguage, |
| 91 | + identSize: Int = 4, |
| 92 | + usesTabsForIndentation: Bool = false |
| 93 | + ) async -> PromptToCodeService { |
| 94 | + let build = { |
| 95 | + let service = PromptToCodeService( |
| 96 | + code: code, |
| 97 | + selectionRange: selectionRange, |
| 98 | + language: language, |
| 99 | + identSize: identSize, |
| 100 | + usesTabsForIndentation: usesTabsForIndentation |
| 101 | + ) |
| 102 | + let provider = PromptToCodeProvider( |
| 103 | + service: service, |
| 104 | + onClosePromptToCode: { [weak self] in |
| 105 | + self?.removePromptToCode(for: url) |
| 106 | + let presenter = PresentInWindowSuggestionPresenter() |
| 107 | + presenter.closePromptToCode(fileURL: url) |
| 108 | + } |
| 109 | + ) |
| 110 | + return PromptToCode(promptToCodeService: service, provider: provider) |
27 | 111 | } |
28 | | - return chat |
| 112 | + |
| 113 | + let newPromptToCode = build() |
| 114 | + promptToCodes[url] = newPromptToCode |
| 115 | + return newPromptToCode.promptToCodeService |
| 116 | + } |
| 117 | + |
| 118 | + func removeChat(for url: URL) { |
| 119 | + chats[url] = nil |
| 120 | + } |
| 121 | + |
| 122 | + func removePromptToCode(for url: URL) { |
| 123 | + promptToCodes[url] = nil |
| 124 | + } |
| 125 | + |
| 126 | + func cleanup(for url: URL) { |
| 127 | + removeChat(for: url) |
| 128 | + removePromptToCode(for: url) |
29 | 129 | } |
30 | 130 | } |
31 | 131 |
|
32 | 132 | extension WidgetDataSource: SuggestionWidgetDataSource { |
33 | 133 | func suggestionForFile(at url: URL) async -> SuggestionProvider? { |
34 | | - for workspace in await workspaces.values { |
35 | | - if let filespace = await workspace.filespaces[url], |
36 | | - let suggestion = await filespace.presentingSuggestion |
| 134 | + for workspace in workspaces.values { |
| 135 | + if let filespace = workspace.filespaces[url], |
| 136 | + let suggestion = filespace.presentingSuggestion |
37 | 137 | { |
38 | 138 | return .init( |
39 | 139 | code: suggestion.text, |
40 | | - language: await filespace.language, |
| 140 | + language: filespace.language, |
41 | 141 | startLineIndex: suggestion.position.line, |
42 | | - suggestionCount: await filespace.suggestions.count, |
43 | | - currentSuggestionIndex: await filespace.suggestionIndex, |
| 142 | + suggestionCount: filespace.suggestions.count, |
| 143 | + currentSuggestionIndex: filespace.suggestionIndex, |
44 | 144 | onSelectPreviousSuggestionTapped: { |
45 | 145 | Task { @ServiceActor in |
46 | 146 | let handler = PseudoCommandHandler() |
@@ -73,49 +173,20 @@ extension WidgetDataSource: SuggestionWidgetDataSource { |
73 | 173 |
|
74 | 174 | func chatForFile(at url: URL) async -> ChatProvider? { |
75 | 175 | let useGlobalChat = UserDefaults.shared.value(for: \.useGlobalChat) |
76 | | - let buildChatProvider = { (service: ChatService) in |
77 | | - ChatProvider( |
78 | | - service: service, |
79 | | - fileURL: url, |
80 | | - onCloseChat: { [weak self] in |
81 | | - if UserDefaults.shared.value(for: \.useGlobalChat) { |
82 | | - self?.globalChat = nil |
83 | | - self?.globalChatProvider = nil |
84 | | - } else { |
85 | | - self?.chats[url] = nil |
86 | | - self?.chatProviders[url] = nil |
87 | | - } |
88 | | - let presenter = PresentInWindowSuggestionPresenter() |
89 | | - presenter.closeChatRoom(fileURL: url) |
90 | | - }, |
91 | | - onSwitchContext: { [weak self] in |
92 | | - let useGlobalChat = UserDefaults.shared.value(for: \.useGlobalChat) |
93 | | - UserDefaults.shared.set(!useGlobalChat, for: \.useGlobalChat) |
94 | | - self?.createChatIfNeeded(for: url) |
95 | | - let presenter = PresentInWindowSuggestionPresenter() |
96 | | - presenter.presentChatRoom(fileURL: url) |
97 | | - } |
98 | | - ) |
99 | | - } |
100 | | - |
101 | 176 | if useGlobalChat { |
102 | | - if let globalChatProvider { |
103 | | - return globalChatProvider |
104 | | - } else if let globalChat { |
105 | | - let new = buildChatProvider(globalChat) |
106 | | - self.globalChatProvider = new |
107 | | - return new |
| 177 | + if let globalChat { |
| 178 | + return globalChat.provider |
108 | 179 | } |
109 | 180 | } else { |
110 | | - if let provider = chatProviders[url] { |
111 | | - return provider |
112 | | - } else if let service = chats[url] { |
113 | | - let new = buildChatProvider(service) |
114 | | - self.chatProviders[url] = new |
115 | | - return new |
| 181 | + if let chat = chats[url] { |
| 182 | + return chat.provider |
116 | 183 | } |
117 | 184 | } |
118 | 185 |
|
119 | 186 | return nil |
120 | 187 | } |
| 188 | + |
| 189 | + func promptToCodeForFile(at url: URL) async -> PromptToCodeProvider? { |
| 190 | + return promptToCodes[url]?.provider |
| 191 | + } |
121 | 192 | } |
0 commit comments