-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add Wayfinder Provider (Local vs Hosted Router) #1966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tcballard
wants to merge
5
commits into
steipete:main
Choose a base branch
from
itsthelore:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9886f2c
Add Wayfinder provider core (descriptor, fetcher, settings reader)
tcballard 7a05588
Add Wayfinder app integration (settings pane, icon, menu and CLI lines)
tcballard 770c692
Add Wayfinder provider tests
tcballard 4fdb0a8
Add Wayfinder provider docs
tcballard 297be11
Fix Wayfinder routed summary to stop assuming the first model is local
tcballard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Sources/CodexBar/Providers/Wayfinder/WayfinderProviderImplementation.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
|
|
||
| struct WayfinderProviderImplementation: ProviderImplementation { | ||
| let id: UsageProvider = .wayfinder | ||
|
|
||
| @MainActor | ||
| func presentation(context _: ProviderPresentationContext) -> ProviderPresentation { | ||
| ProviderPresentation { _ in "api" } | ||
| } | ||
|
|
||
| @MainActor | ||
| func observeSettings(_ settings: SettingsStore) { | ||
| _ = settings.wayfinderGatewayURL | ||
| } | ||
|
|
||
| @MainActor | ||
| func isAvailable(context _: ProviderAvailabilityContext) -> Bool { | ||
| // The gateway's read-only API needs no credentials; enabling the provider is the opt-in. | ||
| true | ||
| } | ||
|
|
||
| @MainActor | ||
| func settingsFields(context: ProviderSettingsContext) -> [ProviderSettingsFieldDescriptor] { | ||
| [ | ||
| ProviderSettingsFieldDescriptor( | ||
| id: "wayfinder-gateway-url", | ||
| title: "Gateway URL", | ||
| subtitle: "Local Wayfinder gateway. Read-only polling of health, routing split, and " + | ||
| "savings — prompts are never read or sent.", | ||
| kind: .plain, | ||
| placeholder: WayfinderSettingsReader.defaultBaseURL.absoluteString, | ||
| binding: context.stringBinding(\.wayfinderGatewayURL), | ||
| actions: [], | ||
| isVisible: nil, | ||
| onActivate: nil), | ||
| ] | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
Sources/CodexBar/Providers/Wayfinder/WayfinderSettingsStore.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
|
|
||
| extension SettingsStore { | ||
| var wayfinderGatewayURL: String { | ||
| get { self.configSnapshot.providerConfig(for: .wayfinder)?.sanitizedEnterpriseHost ?? "" } | ||
| set { | ||
| self.updateProviderConfig(provider: .wayfinder) { entry in | ||
| entry.enterpriseHost = self.normalizedConfigValue(newValue) | ||
| } | ||
| } | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Generated by Scripts/regenerate-codex-parser-hash.sh. Do not edit by hand. | ||
|
|
||
| enum CodexParserHash { | ||
| static let value = "d43faceb2100c02d" | ||
| static let value = "1745966d77d9dae8" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Sources/CodexBarCore/Providers/Wayfinder/WayfinderProviderDescriptor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import Foundation | ||
|
|
||
| public enum WayfinderProviderDescriptor { | ||
| public static let descriptor: ProviderDescriptor = Self.makeDescriptor() | ||
|
|
||
| static func makeDescriptor() -> ProviderDescriptor { | ||
| ProviderDescriptor( | ||
| id: .wayfinder, | ||
| metadata: ProviderMetadata( | ||
| id: .wayfinder, | ||
| displayName: "Wayfinder", | ||
| sessionLabel: "Savings", | ||
| weeklyLabel: "Requests", | ||
| opusLabel: nil, | ||
| supportsOpus: false, | ||
| supportsCredits: false, | ||
| creditsHint: "", | ||
| toggleTitle: "Show Wayfinder usage", | ||
| cliName: "wayfinder", | ||
| defaultEnabled: false, | ||
| dashboardURL: "http://127.0.0.1:8088/router", | ||
| statusPageURL: nil), | ||
| branding: ProviderBranding( | ||
| iconStyle: .wayfinder, | ||
| iconResourceName: "ProviderIcon-wayfinder", | ||
| color: ProviderColor(red: 16 / 255, green: 163 / 255, blue: 127 / 255)), | ||
| tokenCost: ProviderTokenCostConfig( | ||
| supportsTokenCost: false, | ||
| noDataMessage: { "Wayfinder savings are reported by its local gateway." }), | ||
| fetchPlan: ProviderFetchPlan( | ||
| sourceModes: [.auto, .api], | ||
| pipeline: ProviderFetchPipeline(resolveStrategies: { _ in [WayfinderGatewayFetchStrategy()] })), | ||
| cli: ProviderCLIConfig( | ||
| name: "wayfinder", | ||
| aliases: ["wayfinder-router"], | ||
| versionDetector: nil)) | ||
| } | ||
| } | ||
|
|
||
| struct WayfinderGatewayFetchStrategy: ProviderFetchStrategy { | ||
| let id = "wayfinder.api" | ||
| let kind: ProviderFetchKind = .apiToken | ||
|
|
||
| func isAvailable(_: ProviderFetchContext) async -> Bool { | ||
| // The gateway's read-only endpoints are unauthenticated; the provider is | ||
| // opt-in (defaultEnabled: false), so no credential gates availability. | ||
| true | ||
| } | ||
|
|
||
| func fetch(_ context: ProviderFetchContext) async throws -> ProviderFetchResult { | ||
| try WayfinderSettingsReader.validateEndpointOverride(environment: context.env) | ||
| let usage = try await WayfinderUsageFetcher.fetchUsage( | ||
| baseURL: WayfinderSettingsReader.baseURL(environment: context.env)) | ||
| return self.makeResult(usage: usage.toUsageSnapshot(), sourceLabel: "api") | ||
| } | ||
|
|
||
| func shouldFallback(on _: Error, context _: ProviderFetchContext) -> Bool { | ||
| false | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
Sources/CodexBarCore/Providers/Wayfinder/WayfinderSettingsReader.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import Foundation | ||
|
|
||
| public enum WayfinderSettingsError: LocalizedError, Equatable, Sendable { | ||
| case invalidEndpointOverride(String) | ||
|
|
||
| public var errorDescription: String? { | ||
| switch self { | ||
| case let .invalidEndpointOverride(key): | ||
| "Wayfinder gateway URL override \(key) is invalid. Use an HTTPS URL, or plain HTTP for " + | ||
| "loopback addresses only, without embedded credentials." | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public enum WayfinderSettingsReader { | ||
| public static let baseURLEnvironmentKey = "WAYFINDER_GATEWAY_URL" | ||
| public static let defaultBaseURL = URL(string: "http://127.0.0.1:8088")! | ||
|
|
||
| public static func baseURL( | ||
| environment: [String: String] = ProcessInfo.processInfo.environment) -> URL | ||
| { | ||
| guard let raw = self.cleaned(environment[self.baseURLEnvironmentKey]) else { | ||
| return self.defaultBaseURL | ||
| } | ||
| // Loopback HTTP is allowed because the gateway is a local service; the default | ||
| // base URL is plain HTTP on 127.0.0.1. Non-loopback hosts must use HTTPS. | ||
| return ProviderEndpointOverrideValidator().validatedURLAllowingLoopbackHTTP(raw) ?? self.defaultBaseURL | ||
| } | ||
|
|
||
| public static func validateEndpointOverride( | ||
| environment: [String: String] = ProcessInfo.processInfo.environment) throws | ||
| { | ||
| guard let raw = self.cleaned(environment[self.baseURLEnvironmentKey]) else { return } | ||
| guard ProviderEndpointOverrideValidator().validatedURLAllowingLoopbackHTTP(raw) != nil else { | ||
| throw WayfinderSettingsError.invalidEndpointOverride(self.baseURLEnvironmentKey) | ||
| } | ||
| } | ||
|
|
||
| static func cleaned(_ raw: String?) -> String? { | ||
| guard var value = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else { | ||
| return nil | ||
| } | ||
| if (value.hasPrefix("\"") && value.hasSuffix("\"")) || | ||
| (value.hasPrefix("'") && value.hasSuffix("'")) | ||
| { | ||
| value = String(value.dropFirst().dropLast()) | ||
| } | ||
| value = value.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| return value.isEmpty ? nil : value | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user overrides the Wayfinder Gateway URL (for example to
http://localhost:9099), fetches use that override via the projected environment, but the menu'sUsage Dashboardaction falls throughStatusItemController+Actions.dashboardURLto this hard-coded metadata URL for Wayfinder. In that configuration the dashboard button opens the default127.0.0.1:8088instance, which is wrong or unreachable even though polling succeeds against the configured gateway.Useful? React with 👍 / 👎.