-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathAPIProvidedView.swift
More file actions
128 lines (118 loc) · 4.08 KB
/
Copy pathAPIProvidedView.swift
File metadata and controls
128 lines (118 loc) · 4.08 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
124
125
126
127
128
//
// APIProvidedView.swift
// Demo
//
// Created by Sihao Lu on 4/7/23.
//
import DemoChat
import OpenAI
import SwiftUI
struct APIProvidedView: View {
@Binding var apiKey: String
@Binding var providerRaw: String
@Binding var host: String
@Binding var basePath: String
@Binding var githubToken: String
@StateObject var chatStore: ChatStore
@StateObject var imageStore: ImageStore
@StateObject var assistantStore: AssistantStore
@StateObject var miscStore: MiscStore
@StateObject var responsesStore: ResponsesStore
@StateObject var mcpToolsStore: MCPToolsStore
@State var isShowingAPIConfigModal: Bool = true
@Environment(\.idProviderValue) var idProvider
@Environment(\.dateProviderValue) var dateProvider
init(
apiKey: Binding<String>,
providerRaw: Binding<String>,
host: Binding<String>,
basePath: Binding<String>,
githubToken: Binding<String>,
idProvider: @escaping () -> String
) {
self._apiKey = apiKey
self._providerRaw = providerRaw
self._host = host
self._basePath = basePath
self._githubToken = githubToken
let client = APIProvidedView.makeClient(
apiKey: apiKey.wrappedValue,
host: host.wrappedValue,
basePath: basePath.wrappedValue
)
self._chatStore = StateObject(
wrappedValue: ChatStore(
openAIClient: client,
idProvider: idProvider
)
)
self._imageStore = StateObject(
wrappedValue: ImageStore(
openAIClient: client
)
)
self._assistantStore = StateObject(
wrappedValue: AssistantStore(
openAIClient: client,
idProvider: idProvider
)
)
self._miscStore = StateObject(
wrappedValue: MiscStore(
openAIClient: client
)
)
self._responsesStore = StateObject(
wrappedValue: ResponsesStore(
client: OpenAI(
configuration: APIProvidedView.makeConfiguration(
apiKey: apiKey.wrappedValue,
host: host.wrappedValue,
basePath: basePath.wrappedValue
),
middlewares: [LoggingMiddleware()]
).responses
)
)
self._mcpToolsStore = StateObject(
wrappedValue: MCPToolsStore(githubToken: githubToken)
)
}
var body: some View {
ContentView(
chatStore: chatStore,
imageStore: imageStore,
assistantStore: assistantStore,
miscStore: miscStore,
responsesStore: responsesStore,
mcpToolsStore: mcpToolsStore
)
.onAppear {
// Connect MCP tools store to responses store
responsesStore.mcpToolsStore = mcpToolsStore
}
.onChange(of: apiKey) { _, _ in rewireClient() }
.onChange(of: host) { _, _ in rewireClient() }
.onChange(of: basePath) { _, _ in rewireClient() }
}
private func rewireClient() {
let client = APIProvidedView.makeClient(
apiKey: apiKey,
host: host,
basePath: basePath
)
chatStore.openAIClient = client
imageStore.openAIClient = client
assistantStore.openAIClient = client
miscStore.openAIClient = client
responsesStore.client = client.responses
}
private static func makeClient(apiKey: String, host: String, basePath: String) -> OpenAIProtocol {
OpenAI(configuration: makeConfiguration(apiKey: apiKey, host: host, basePath: basePath))
}
private static func makeConfiguration(apiKey: String, host: String, basePath: String) -> OpenAI.Configuration {
let resolvedHost = host.isEmpty ? "api.openai.com" : host
let resolvedBasePath = basePath.isEmpty ? "/v1" : basePath
return OpenAI.Configuration(token: apiKey, host: resolvedHost, basePath: resolvedBasePath)
}
}