forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatTabFactory.swift
More file actions
124 lines (115 loc) · 4.76 KB
/
ChatTabFactory.swift
File metadata and controls
124 lines (115 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import ChatGPTChatTab
import ChatService
import ChatTab
import Foundation
import PromptToCodeService
import SuggestionModel
import SuggestionWidget
import XcodeInspector
#if canImport(ProChatTabs)
import ProChatTabs
enum ChatTabFactory {
static func chatTabBuilderCollection(
openTab: @escaping (any ChatTab) -> Void
) -> [ChatTabBuilderCollection] {
func folderIfNeeded(
_ builders: [any ChatTabBuilder],
title: String
) -> ChatTabBuilderCollection? {
if builders.count > 1 {
return .folder(title: title, kinds: builders.map(ChatTabKind.init))
}
if let first = builders.first { return .kind(ChatTabKind(first)) }
return nil
}
let collection = [
folderIfNeeded(ChatGPTChatTab.chatBuilders(), title: ChatGPTChatTab.name),
folderIfNeeded(BrowserChatTab.chatBuilders(externalDependency: .init(
getEditorContent: {
guard let editor = XcodeInspector.shared.focusedEditor else {
return .init(selectedText: "", language: "", fileContent: "")
}
let content = editor.content
return .init(
selectedText: content.selectedContent,
language: languageIdentifierFromFileURL(
XcodeInspector.shared
.activeDocumentURL
)
.rawValue,
fileContent: content.content
)
},
handleCustomCommand: { command, prompt in
switch command.feature {
case let .chatWithSelection(extraSystemPrompt, _, useExtraSystemPrompt):
let service = ChatService()
return try await service.processMessage(
systemPrompt: nil,
extraSystemPrompt: (useExtraSystemPrompt ?? false) ? extraSystemPrompt :
nil,
prompt: prompt
)
case let .customChat(systemPrompt, _):
let service = ChatService()
return try await service.processMessage(
systemPrompt: systemPrompt,
extraSystemPrompt: nil,
prompt: prompt
)
case let .singleRoundDialog(
systemPrompt,
overwriteSystemPrompt,
_,
_
):
let service = ChatService()
return try await service.handleSingleRoundDialogCommand(
systemPrompt: systemPrompt,
overwriteSystemPrompt: overwriteSystemPrompt ?? false,
prompt: prompt
)
case let .promptToCode(extraSystemPrompt, instruction, _, _):
let service = PromptToCodeService(
code: prompt,
selectionRange: .outOfScope,
language: .plaintext,
identSize: 4,
usesTabsForIndentation: true,
projectRootURL: .init(fileURLWithPath: "/"),
fileURL: .init(fileURLWithPath: "/"),
allCode: prompt,
extraSystemPrompt: extraSystemPrompt,
generateDescriptionRequirement: false
)
try await service.modifyCode(prompt: instruction ?? "Modify content.")
return service.code
}
},
handleNewTab: openTab
)), title: BrowserChatTab.name),
].compactMap { $0 }
return collection
}
}
#else
enum ChatTabFactory {
static func chatTabBuilderCollection(
openTab: @escaping (any ChatTab) -> Void
) -> [ChatTabBuilderCollection] {
func folderIfNeeded(
_ builders: [any ChatTabBuilder],
title: String
) -> ChatTabBuilderCollection? {
if builders.count > 1 {
return .folder(title: title, kinds: builders.map(ChatTabKind.init))
}
if let first = builders.first { return .kind(ChatTabKind(first)) }
return nil
}
return [
folderIfNeeded(ChatGPTChatTab.chatBuilders(), title: ChatGPTChatTab.name),
].compactMap { $0 }
}
}
#endif