Skip to content

Commit b68fb40

Browse files
committed
Expose chat tabs to widget
1 parent 2eedc62 commit b68fb40

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

Core/Sources/Service/GUI/ChatTabFactory.swift

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BuiltinExtension
12
import ChatGPTChatTab
23
import ChatService
34
import ChatTab
@@ -9,20 +10,11 @@ import XcodeInspector
910

1011
#if canImport(ProChatTabs)
1112
import ProChatTabs
13+
#endif
1214

1315
enum ChatTabFactory {
1416
static func chatTabBuilderCollection() -> [ChatTabBuilderCollection] {
15-
func folderIfNeeded(
16-
_ builders: [any ChatTabBuilder],
17-
title: String
18-
) -> ChatTabBuilderCollection? {
19-
if builders.count > 1 {
20-
return .folder(title: title, kinds: builders.map(ChatTabKind.init))
21-
}
22-
if let first = builders.first { return .kind(ChatTabKind(first)) }
23-
return nil
24-
}
25-
17+
#if canImport(ProChatTabs)
2618
let collection = [
2719
folderIfNeeded(ChatGPTChatTab.chatBuilders(), title: ChatGPTChatTab.name),
2820
folderIfNeeded(
@@ -32,11 +24,34 @@ enum ChatTabFactory {
3224
title: BrowserChatTab.name
3325
),
3426
folderIfNeeded(TerminalChatTab.chatBuilders(), title: TerminalChatTab.name),
35-
].compactMap { $0 }
27+
]
28+
#else
29+
let collection = [
30+
folderIfNeeded(ChatGPTChatTab.chatBuilders(), title: ChatGPTChatTab.name),
31+
]
32+
#endif
3633

37-
return collection
34+
return collection.compactMap { $0 } + chatTabsFromExtensions()
3835
}
3936

37+
private static func folderIfNeeded(
38+
_ builders: [any ChatTabBuilder],
39+
title: String
40+
) -> ChatTabBuilderCollection? {
41+
if builders.count > 1 {
42+
return .folder(title: title, kinds: builders.map(ChatTabKind.init))
43+
}
44+
if let first = builders.first { return .kind(ChatTabKind(first)) }
45+
return nil
46+
}
47+
48+
static func chatTabsFromExtensions() -> [ChatTabBuilderCollection] {
49+
let extensions = BuiltinExtensionManager.shared.extensions
50+
let chatBuilders = extensions.flatMap { $0.chatBuilders }
51+
return chatBuilders.compactMap { folderIfNeeded($0.value, title: $0.key) }
52+
}
53+
54+
#if canImport(ProChatTabs)
4055
static func externalDependenciesForBrowserChatTab() -> BrowserChatTab.ExternalDependency {
4156
.init(
4257
getEditorContent: {
@@ -109,28 +124,6 @@ enum ChatTabFactory {
109124
}
110125
)
111126
}
127+
#endif
112128
}
113129

114-
#else
115-
116-
enum ChatTabFactory {
117-
static func chatTabBuilderCollection() -> [ChatTabBuilderCollection] {
118-
func folderIfNeeded(
119-
_ builders: [any ChatTabBuilder],
120-
title: String
121-
) -> ChatTabBuilderCollection? {
122-
if builders.count > 1 {
123-
return .folder(title: title, kinds: builders.map(ChatTabKind.init))
124-
}
125-
if let first = builders.first { return .kind(ChatTabKind(first)) }
126-
return nil
127-
}
128-
129-
return [
130-
folderIfNeeded(ChatGPTChatTab.chatBuilders(), title: ChatGPTChatTab.name),
131-
].compactMap { $0 }
132-
}
133-
}
134-
135-
#endif
136-

Tool/Sources/BuiltinExtension/BuiltinExtension.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ChatTab
12
import CopilotForXcodeKit
23
import Foundation
34
import Preferences
@@ -6,8 +7,17 @@ public protocol BuiltinExtension: CopilotForXcodeExtensionCapability {
67
/// An id that let the extension manager determine whether the extension is in use.
78
var suggestionServiceId: BuiltInSuggestionFeatureProvider { get }
89

10+
/// All chat builders provided by this extension.
11+
var chatBuilders: [String: [ChatTabBuilder]] { get }
12+
913
/// It's usually called when the app is about to quit,
1014
/// you should clean up all the resources here.
1115
func terminate()
1216
}
1317

18+
// MARK: - Default Implementation
19+
20+
public extension BuiltinExtension {
21+
var chatBuilders: [String: [ChatTabBuilder]] { [:] }
22+
}
23+

Tool/Sources/BuiltinExtension/BuiltinExtensionManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import XcodeInspector
55

66
public final class BuiltinExtensionManager {
77
public static let shared: BuiltinExtensionManager = .init()
8-
private(set) var extensions: [any BuiltinExtension] = []
8+
public private(set) var extensions: [any BuiltinExtension] = []
99

1010
private var cancellable: Set<AnyCancellable> = []
1111

0 commit comments

Comments
 (0)