forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubCopilotModelPicker.swift
More file actions
91 lines (81 loc) · 2.81 KB
/
Copy pathGitHubCopilotModelPicker.swift
File metadata and controls
91 lines (81 loc) · 2.81 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
import Dependencies
import Foundation
import GitHubCopilotService
import Perception
import SwiftUI
import Toast
public struct GitHubCopilotModelPicker: View {
@Perceptible
final class ViewModel {
var availableModels: [GitHubCopilotLLMModel] = []
@PerceptionIgnored @Dependency(\.toast) var toast
init() {}
func appear() {
reloadAvailableModels()
}
func disappear() {}
func reloadAvailableModels() {
Task { @MainActor in
do {
availableModels = try await GitHubCopilotExtension.fetchLLMModels()
} catch {
toast("Failed to fetch GitHub Copilot models: \(error)", .error)
}
}
}
}
let title: String
let hasDefaultModel: Bool
@Binding var gitHubCopilotModelId: String
@State var viewModel: ViewModel
init(
title: String,
hasDefaultModel: Bool = true,
gitHubCopilotModelId: Binding<String>
) {
self.title = title
_gitHubCopilotModelId = gitHubCopilotModelId
self.hasDefaultModel = hasDefaultModel
viewModel = .init()
}
public var body: some View {
WithPerceptionTracking {
TextField(title, text: $gitHubCopilotModelId)
.overlay(alignment: .trailing) {
Picker(
"",
selection: $gitHubCopilotModelId,
content: {
if hasDefaultModel {
Text("Default").tag("")
}
if !gitHubCopilotModelId.isEmpty,
!viewModel.availableModels.contains(where: {
$0.modelId == gitHubCopilotModelId
})
{
Text(gitHubCopilotModelId).tag(gitHubCopilotModelId)
}
if viewModel.availableModels.isEmpty {
Text({
viewModel.reloadAvailableModels()
return "Loading..."
}()).tag("Loading...")
}
ForEach(viewModel.availableModels) { model in
Text(model.modelId)
.tag(model.modelId)
}
}
)
.frame(width: 20)
}
.onAppear {
viewModel.appear()
}
.onDisappear {
viewModel.disappear()
}
}
}
}