Skip to content

Open source CapCut alternative.

TypeScript

Support packaged backend inference URL

David Le94d6e08d3187
diff --git a/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift b/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
index 86686ae9..201da31c 100644
--- a/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
+++ b/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
@@ -13,6 +13,7 @@ public enum DonkeyBackendInferenceClientError: Error, Equatable, Sendable {
public struct DonkeyBackendInferenceConfiguration: Equatable, Sendable {
public var baseURL: URL
public var clientID: String
+ public static let baseURLConfigurationDescription = "DONKEY_BACKEND_URL, DONKEY_WEB_BASE_URL, or BETTER_AUTH_URL"
public init(baseURL: URL, clientID: String) {
self.baseURL = baseURL
@@ -20,11 +21,17 @@ public struct DonkeyBackendInferenceConfiguration: Equatable, Sendable {
}
public static func fromEnvironment(
- _ environment: [String: String] = ProcessInfo.processInfo.environment
+ _ environment: [String: String] = ProcessInfo.processInfo.environment,
+ bundle: Bundle = .main
) throws -> DonkeyBackendInferenceConfiguration {
- guard let baseURLString = environment["DONKEY_BACKEND_URL"],
+ guard let baseURLString = configuredBaseURLString(
+ environment: environment,
+ bundle: bundle
+ ),
let baseURL = URL(string: baseURLString) else {
- throw DonkeyBackendInferenceClientError.missingConfiguration("DONKEY_BACKEND_URL")
+ throw DonkeyBackendInferenceClientError.missingConfiguration(
+ baseURLConfigurationDescription
+ )
}
return DonkeyBackendInferenceConfiguration(
@@ -33,6 +40,30 @@ public struct DonkeyBackendInferenceConfiguration: Equatable, Sendable {
)
}
+ private static func configuredBaseURLString(
+ environment: [String: String],
+ bundle: Bundle
+ ) -> String? {
+ trimmed(environment["DONKEY_BACKEND_URL"])
+ ?? trimmed(environment["DONKEY_WEB_BASE_URL"])
+ ?? trimmed(environment["BETTER_AUTH_URL"])
+ ?? configuredBundleValue("DonkeyBackendURL", bundle: bundle)
+ ?? configuredBundleValue("DonkeyWebBaseURL", bundle: bundle)
+ }
+
+ private static func configuredBundleValue(
+ _ key: String,
+ bundle: Bundle
+ ) -> String? {
+ let bundleValue = bundle.object(forInfoDictionaryKey: key) as? String
+ return trimmed(bundleValue)
+ }
+
+ private static func trimmed(_ value: String?) -> String? {
+ let trimmedValue = value?.trimmingCharacters(in: .whitespacesAndNewlines)
+ return trimmedValue?.isEmpty == false ? trimmedValue : nil
+ }
+
private static func stableClientID() -> String {
let key = "DonkeyBackendInferenceClientID"
if let existing = UserDefaults.standard.string(forKey: key), !existing.isEmpty {
diff --git a/apps/Donkey/Sources/DonkeyAI/HostedPlannerHintAdapter.swift b/apps/Donkey/Sources/DonkeyAI/HostedPlannerHintAdapter.swift
index c4f493ed..bab28f44 100644
--- a/apps/Donkey/Sources/DonkeyAI/HostedPlannerHintAdapter.swift
+++ b/apps/Donkey/Sources/DonkeyAI/HostedPlannerHintAdapter.swift
@@ -173,7 +173,7 @@ public struct HostedPlannerHintAdapter: Sendable {
validationStatus: "notValidated",
latencyMS: nil,
metadata: [
- "credential": "DONKEY_BACKEND_URL",
+ "credential": DonkeyBackendInferenceConfiguration.baseURLConfigurationDescription,
"error": String(describing: error)
]
)
diff --git a/apps/Donkey/Sources/DonkeyAI/LocalGenerateTaskIntentAdapter.swift b/apps/Donkey/Sources/DonkeyAI/LocalGenerateTaskIntentAdapter.swift
index a04b7c90..5d88003f 100644
--- a/apps/Donkey/Sources/DonkeyAI/LocalGenerateTaskIntentAdapter.swift
+++ b/apps/Donkey/Sources/DonkeyAI/LocalGenerateTaskIntentAdapter.swift
@@ -753,7 +753,7 @@ public struct HostedTaskIntentParsingAdapter: TaskIntentParsingAdapter {
validationStatus: "notValidated",
latencyMS: nil,
metadata: [
- "credential": "DONKEY_BACKEND_URL",
+ "credential": DonkeyBackendInferenceConfiguration.baseURLConfigurationDescription,
"error": String(describing: error)
]
)
diff --git a/apps/Donkey/Sources/DonkeyAI/LocalTaskFollowUpResolver.swift b/apps/Donkey/Sources/DonkeyAI/LocalTaskFollowUpResolver.swift
index 5550031f..5428b0b5 100644
--- a/apps/Donkey/Sources/DonkeyAI/LocalTaskFollowUpResolver.swift
+++ b/apps/Donkey/Sources/DonkeyAI/LocalTaskFollowUpResolver.swift
@@ -291,7 +291,7 @@ public struct HostedTaskFollowUpResolver: PointerPromptFollowUpResolving {
validationStatus: "notValidated",
latencyMS: nil,
metadata: [
- "credential": "DONKEY_BACKEND_URL",
+ "credential": DonkeyBackendInferenceConfiguration.baseURLConfigurationDescription,
"error": String(describing: error)
]
)
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/DonkeyBackendInferenceClientTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/DonkeyBackendInferenceClientTests.swift
index 483a2a5e..e42c67ca 100644
--- a/apps/Donkey/Tests/DonkeyRuntimeTests/DonkeyBackendInferenceClientTests.swift
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/DonkeyBackendInferenceClientTests.swift
@@ -64,6 +64,68 @@ struct DonkeyBackendInferenceClientTests {
#expect(object["stream"] as? Bool == false)
}
+ @Test
+ func configurationPrefersDedicatedBackendEnvironmentURL() throws {
+ let configuration = try DonkeyBackendInferenceConfiguration.fromEnvironment([
+ "DONKEY_BACKEND_URL": "https://api.donkey.example",
+ "DONKEY_WEB_BASE_URL": "https://web.donkey.example",
+ "DONKEY_CLIENT_ID": "client-env"
+ ])
+
+ #expect(configuration.baseURL.absoluteString == "https://api.donkey.example")
+ #expect(configuration.clientID == "client-env")
+ }
+
+ @Test
+ func configurationFallsBackToWebBaseEnvironmentURL() throws {
+ let configuration = try DonkeyBackendInferenceConfiguration.fromEnvironment([
+ "DONKEY_WEB_BASE_URL": "https://web.donkey.example",
+ "DONKEY_CLIENT_ID": "client-env"
+ ])
+
+ #expect(configuration.baseURL.absoluteString == "https://web.donkey.example")
+ #expect(configuration.clientID == "client-env")
+ }
+
+ @Test
+ func configurationEnvironmentOverridesBundledBackendURL() throws {
+ let fixture = try temporaryBundle(info: [
+ "DonkeyBackendURL": "https://bundle-api.donkey.example"
+ ])
+ defer {
+ try? FileManager.default.removeItem(at: fixture.directory)
+ }
+
+ let configuration = try DonkeyBackendInferenceConfiguration.fromEnvironment(
+ [
+ "DONKEY_WEB_BASE_URL": "https://web.donkey.example",
+ "DONKEY_CLIENT_ID": "client-env"
+ ],
+ bundle: fixture.bundle
+ )
+
+ #expect(configuration.baseURL.absoluteString == "https://web.donkey.example")
+ #expect(configuration.clientID == "client-env")
+ }
+
+ @Test
+ func configurationFallsBackToBundledWebBaseURL() throws {
+ let fixture = try temporaryBundle(info: [
+ "DonkeyWebBaseURL": "https://bundle.donkey.example"
+ ])
+ defer {
+ try? FileManager.default.removeItem(at: fixture.directory)
+ }
+
+ let configuration = try DonkeyBackendInferenceConfiguration.fromEnvironment(
+ ["DONKEY_CLIENT_ID": "client-env"],
+ bundle: fixture.bundle
+ )
+
+ #expect(configuration.baseURL.absoluteString == "https://bundle.donkey.example")
+ #expect(configuration.clientID == "client-env")
+ }
+
@Test
func decodesServerSentEvents() {
let data = Data(
@@ -232,6 +294,33 @@ struct DonkeyBackendInferenceClientTests {
)
}
+ private func temporaryBundle(info: [String: String]) throws -> (bundle: Bundle, directory: URL) {
+ let directory = FileManager.default.temporaryDirectory
+ .appendingPathComponent("donkey-bundle-tests-\(UUID().uuidString)", isDirectory: true)
+ let bundleURL = directory.appendingPathComponent("DonkeyTest.bundle", isDirectory: true)
+ try FileManager.default.createDirectory(at: bundleURL, withIntermediateDirectories: true)
+
+ var plist: [String: Any] = [
+ "CFBundleIdentifier": "ai.donkey.tests.\(UUID().uuidString)",
+ "CFBundlePackageType": "BNDL"
+ ]
+ for (key, value) in info {
+ plist[key] = value
+ }
+
+ let data = try PropertyListSerialization.data(
+ fromPropertyList: plist,
+ format: .xml,
+ options: 0
+ )
+ try data.write(to: bundleURL.appendingPathComponent("Info.plist"))
+
+ return (
+ bundle: try #require(Bundle(url: bundleURL)),
+ directory: directory
+ )
+ }
+
private func generationRecord(
outputs: [RemoteInferenceOutputRef]
) -> RemoteInferenceGenerationRecord {
diff --git a/docs/guides/install-donkey.md b/docs/guides/install-donkey.md
index 5bb23ed2..bbc0c706 100644
--- a/docs/guides/install-donkey.md
+++ b/docs/guides/install-donkey.md
@@ -21,13 +21,23 @@ DONKEY_APP_VERSION="0.1.1" DONKEY_APP_BUILD="2" ./scripts/package-donkey-app.sh
The app bundle registers the `donkey://auth/callback` sign-in callback and
opens the site `/mac-auth` handoff for Google OAuth before the overlay starts.
-Packaged apps point at `https://donkeyuse.com` by default. Override the web base
-URL only for local or staging auth testing:
+Packaged apps point auth and hosted inference at `https://donkeyuse.com` by
+default. Override the web base URL only for local or staging testing:
```bash
DONKEY_WEB_BASE_URL="http://localhost:3000" ./scripts/package-donkey-app.sh
```
+If hosted inference is served from a different origin than auth, set
+`DONKEY_BACKEND_URL`; otherwise the packaged app uses `DONKEY_WEB_BASE_URL` for
+both:
+
+```bash
+DONKEY_WEB_BASE_URL="https://staging.example" \
+DONKEY_BACKEND_URL="https://api.staging.example" \
+./scripts/package-donkey-app.sh
+```
+
For distribution through the site, publish the disk image:
```text
diff --git a/scripts/package-donkey-app.sh b/scripts/package-donkey-app.sh
index b34bff44..e87f2336 100755
--- a/scripts/package-donkey-app.sh
+++ b/scripts/package-donkey-app.sh
@@ -10,6 +10,7 @@ RUNTIME_PACKAGE_DIR="$ROOT_DIR/dist/LocalRuntimePackages"
APP_VERSION="${DONKEY_APP_VERSION:-0.1.0}"
APP_BUILD="${DONKEY_APP_BUILD:-1}"
WEB_BASE_URL="${DONKEY_WEB_BASE_URL:-https://donkeyuse.com}"
+BACKEND_BASE_URL="${DONKEY_BACKEND_URL:-$WEB_BASE_URL}"
AUTH_CALLBACK_SCHEME="${DONKEY_AUTH_CALLBACK_SCHEME:-donkey}"
SPARKLE_FEED_URL="${DONKEY_SPARKLE_FEED_URL:-}"
SPARKLE_PUBLIC_ED_KEY="${DONKEY_SPARKLE_PUBLIC_ED_KEY:-}"
@@ -221,6 +222,8 @@ cat > "$CONTENTS_DIR/Info.plist" <<PLIST
<string>14.0</string>
<key>DonkeyWebBaseURL</key>
<string>$WEB_BASE_URL</string>
+ <key>DonkeyBackendURL</key>
+ <string>$BACKEND_BASE_URL</string>
<key>DonkeyAuthCallbackScheme</key>
<string>$AUTH_CALLBACK_SCHEME</string>
<key>CFBundleURLTypes</key>