forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopBackendEnvironment.swift
More file actions
215 lines (191 loc) · 7.66 KB
/
Copy pathDesktopBackendEnvironment.swift
File metadata and controls
215 lines (191 loc) · 7.66 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import Foundation
enum DesktopBackendEnvironment {
static let productionPythonAPIURL = "https://api.omi.me/"
static let productionRustBackendURL = "https://desktop-backend-hhibjajaja-uc.a.run.app/"
static let developmentPythonAPIURL = "https://api.omiapi.com/"
static let developmentRustBackendURL = "https://desktop-backend-dt5lrfkkoa-uc.a.run.app/"
/// Public web share origin (conversation / chat / task links). Override with ``OMI_SHARE_BASE_URL``.
static let productionShareBaseURL = "https://h.omi.me"
static var shouldUseDevelopmentBackends: Bool {
shouldUseDevelopmentBackends(
bundleIdentifier: AppBuild.bundleIdentifier,
updateChannel: AppBuild.currentUpdateChannel,
externalPreviewBackend: AppBuild.externalPreviewBackend
)
}
static func shouldUseDevelopmentBackends(
bundleIdentifier: String,
updateChannel: String,
externalPreviewBackend: AppBuild.ExternalPreviewBackend? = nil
) -> Bool {
// External previews opt into their backend through signed bundle metadata. They must
// never inherit local-development routing or an environment force override. Missing or
// malformed preview metadata therefore fails closed to the production backend.
if AppBuild.isExternalPreviewBundleIdentifier(bundleIdentifier) {
return externalPreviewBackend == .development
}
// Beta is the production-account dogfood channel: it uses the development
// serving plane while retaining production Auth/Firebase/Firestore. This is
// an identity-bound routing rule, never an update-channel or environment
// override. Stable remains pinned to the production serving plane.
if bundleIdentifier == AppBuild.betaProductionBundleIdentifier {
return true
}
// Named/dev bundles route to the dev backend by default. Explicit launch
// URLs still win below so local harnesses and intentionally-targeted tests
// remain possible.
if !AppBuild.productionFamilyBundleIdentifiers.contains(bundleIdentifier) {
return true
}
return false
}
static var shouldUseProductionAuth: Bool {
shouldUseProductionAuth(bundleIdentifier: AppBuild.bundleIdentifier)
}
static func shouldUseProductionAuth(bundleIdentifier: String) -> Bool {
// The shared Firebase project and registered OAuth callback live on the
// production authority. Beta must never inherit a dev auth override while
// its data-serving endpoints intentionally target development.
AppBuild.productionFamilyBundleIdentifiers.contains(bundleIdentifier)
}
static var shouldForceDevelopmentServingEndpoints: Bool {
shouldForceDevelopmentServingEndpoints(bundleIdentifier: AppBuild.bundleIdentifier)
}
static func shouldForceDevelopmentServingEndpoints(bundleIdentifier: String) -> Bool {
bundleIdentifier == AppBuild.betaProductionBundleIdentifier
}
static func pythonBaseURL(
environmentValue: String? = currentEnvironmentValue("OMI_PYTHON_API_URL")
) -> String {
pythonBaseURL(
useDevelopmentBackends: shouldUseDevelopmentBackends,
environmentValue: environmentValue
)
}
static func pythonBaseURL(
useDevelopmentBackends: Bool,
bundleIdentifier: String = AppBuild.bundleIdentifier,
environmentValue: String?
) -> String {
// A production-family app must not allow a launch environment or bundled
// config to switch its customer data plane. Development identities retain
// their explicit override seam for local and signed-preview testing.
if shouldForceDevelopmentServingEndpoints(bundleIdentifier: bundleIdentifier) {
return developmentPythonAPIURL
}
if shouldUseProductionAuth(bundleIdentifier: bundleIdentifier) {
return productionPythonAPIURL
}
if !useDevelopmentBackends {
return productionPythonAPIURL
}
if let url = normalizedURL(environmentValue) {
return url
}
return developmentPythonAPIURL
}
static func authBaseURL(
useDevelopmentBackends: Bool = shouldUseDevelopmentBackends,
bundleIdentifier: String = AppBuild.bundleIdentifier,
environmentValue: String? = currentEnvironmentValue("OMI_AUTH_API_URL")
) -> String {
if shouldUseProductionAuth(bundleIdentifier: bundleIdentifier) || !useDevelopmentBackends {
return productionPythonAPIURL
}
if let url = normalizedURL(environmentValue) {
return url
}
// Desktop Apple Sign-In uses the shared Services ID. The registered web
// callback is on api.omi.me, so beta must not inherit the dev data backend
// host for OAuth unless a local/dev auth URL is explicitly supplied.
return productionPythonAPIURL
}
static func rustBackendURL(
environmentValue: String? = currentEnvironmentValue("OMI_DESKTOP_API_URL"),
launchEnvironmentValue: String? = ProcessInfo.processInfo.environment["OMI_DESKTOP_API_URL"]
) -> String {
rustBackendURL(
useDevelopmentBackends: shouldUseDevelopmentBackends,
environmentValue: environmentValue,
launchEnvironmentValue: launchEnvironmentValue
)
}
static func rustBackendURL(
useDevelopmentBackends: Bool,
bundleIdentifier: String = AppBuild.bundleIdentifier,
environmentValue: String?,
launchEnvironmentValue: String?
) -> String {
if shouldForceDevelopmentServingEndpoints(bundleIdentifier: bundleIdentifier) {
return developmentRustBackendURL
}
if shouldUseProductionAuth(bundleIdentifier: bundleIdentifier) {
return productionRustBackendURL
}
if !useDevelopmentBackends {
return productionRustBackendURL
}
if let url = normalizedURL(environmentValue) {
return url
}
if let url = normalizedURL(launchEnvironmentValue) {
return url
}
return developmentRustBackendURL
}
/// Public share origin used when minting conversation links (#4339).
/// Matches backend ``OMI_SHARE_BASE_URL`` (default ``https://h.omi.me``).
static func shareBaseURL(
environmentValue: String? = currentEnvironmentValue("OMI_SHARE_BASE_URL")
) -> String {
guard var raw = environmentValue?.trimmingCharacters(in: .whitespacesAndNewlines),
!raw.isEmpty
else {
return productionShareBaseURL
}
if !raw.contains("://") {
raw = "https://\(raw)"
}
while raw.hasSuffix("/") {
raw.removeLast()
}
guard let url = URL(string: raw),
let scheme = url.scheme?.lowercased(),
["http", "https"].contains(scheme),
let host = url.host,
!host.isEmpty
else {
return productionShareBaseURL
}
return raw
}
static func conversationShareURL(
id: String,
environmentValue: String? = currentEnvironmentValue("OMI_SHARE_BASE_URL")
) -> String {
"\(shareBaseURL(environmentValue: environmentValue))/conversations/\(id)"
}
static func applyReleaseChannelDefaults() {
if shouldUseDevelopmentBackends {
if normalizedURL(currentEnvironmentValue("OMI_PYTHON_API_URL")) == nil {
setenv("OMI_PYTHON_API_URL", developmentPythonAPIURL, 1)
}
if normalizedURL(currentEnvironmentValue("OMI_DESKTOP_API_URL")) == nil {
setenv("OMI_DESKTOP_API_URL", developmentRustBackendURL, 1)
}
}
log("BackendEnvironment: release-channel defaults applied only for missing backend URLs")
}
private static func normalizedURL(_ raw: String?) -> String? {
guard let raw else { return nil }
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return nil }
return trimmed.hasSuffix("/") ? trimmed : trimmed + "/"
}
private static func currentEnvironmentValue(_ key: String) -> String? {
guard let value = getenv(key), let string = String(validatingCString: value) else {
return nil
}
return string
}
}