Repository navigation
Feed the launcher model picker from the server catalog #909
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
44b9529
Feed the launcher model picker from the server catalog (#900)
realtonyyoung 284f582
Reload the model catalog after sign-in and log unexpected fetch fault…
realtonyyoung b8df948
Guard the model catalog against out-of-order loads (#900)
realtonyyoung 4b8cae7
Make the catalog generation check and publish atomic (#900)
realtonyyoung 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| using System.Collections.Frozen; | ||
| using System.Net.Http.Json; | ||
| using System.Reactive.Subjects; | ||
| using Capacitor.Cli.Core; | ||
| using Capacitor.Cli.Core.Config; | ||
| using Capacitor.Cli.Core.Http; | ||
| using Capacitor.Remote.Models; | ||
|
|
||
| namespace Capacitor.App.Services; | ||
|
|
||
| /// The per-vendor model catalog the server offers for launches, fetched once from | ||
| /// GET api/agents/model-options — the same source the web UI's model dropdown reads. A failed or | ||
| /// signed-out fetch leaves the last snapshot (empty until the first success), and the launcher | ||
| /// falls back to its curated per-vendor list, so a launch is never blocked on this. | ||
| public sealed class ServerVendorModelCatalog : IDisposable { | ||
| public static readonly IReadOnlyDictionary<string, IReadOnlyList<ModelChoice>> Empty = | ||
| FrozenDictionary<string, IReadOnlyList<ModelChoice>>.Empty; | ||
|
|
||
| readonly Func<CancellationToken, Task<IReadOnlyDictionary<string, IReadOnlyList<ModelChoice>>?>> _fetch; | ||
| readonly BehaviorSubject<IReadOnlyDictionary<string, IReadOnlyList<ModelChoice>>> _catalog = new(Empty); | ||
| // Guards the generation counter AND the post-fetch check+publish as one critical section, so a | ||
| // newer load can never slip its publish between an older load's check and its OnNext. | ||
| readonly Lock _gate = new(); | ||
| int _generation; | ||
|
|
||
| public ServerVendorModelCatalog( | ||
| Func<CancellationToken, Task<IReadOnlyDictionary<string, IReadOnlyList<ModelChoice>>?>> fetch) => | ||
| _fetch = fetch; | ||
|
|
||
| public IObservable<IReadOnlyDictionary<string, IReadOnlyList<ModelChoice>>> Catalog => _catalog; | ||
|
|
||
| /// Fetches and publishes on success; a null result (offline / signed out / non-success) leaves | ||
| /// the current snapshot untouched. Cancellation (shutdown) is silent; an unexpected fault is | ||
| /// logged rather than swallowed, but never propagates — the launcher's curated fallback covers a | ||
| /// miss either way. Safe to call more than once concurrently (startup plus a reload per sign-in): | ||
| /// only the newest-started load publishes, so a slow earlier fetch cannot overwrite a newer | ||
| /// snapshot with a stale one. | ||
| public async Task LoadAsync(CancellationToken ct = default) { | ||
| int generation; | ||
| lock (_gate) generation = ++_generation; | ||
| try { | ||
| if (await _fetch(ct).ConfigureAwait(false) is { } catalog) | ||
| lock (_gate) | ||
| if (_generation == generation) _catalog.OnNext(catalog); | ||
| } catch (OperationCanceledException) { | ||
| // Shutdown or a superseded reload — not a failure. | ||
| } catch (Exception ex) { | ||
| // Unexpected (e.g. a deserialization/mapping defect): keep the curated fallback, but do | ||
| // not fail silently — this is the one place such a bug would otherwise be invisible. | ||
| Console.Error.WriteLine($"kcap app: vendor model catalog load failed: {ex}"); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() => _catalog.Dispose(); | ||
|
|
||
| /// Builds the HTTP fetch over an authenticated client, mapping the server's {value,label} to | ||
| /// ModelChoice. Returns null for an EXPECTED miss — signed out, offline, or a non-success status. | ||
| /// An UNEXPECTED fault (a bad payload / mapping bug) propagates to LoadAsync, which logs it rather | ||
| /// than letting a real defect hide behind the curated fallback. | ||
| public static Func<CancellationToken, Task<IReadOnlyDictionary<string, IReadOnlyList<ModelChoice>>?>> | ||
| HttpFetch(ICapacitorHttpClient? http, ProfileContext? profiles) => async ct => { | ||
| var serverUrl = profiles?.Resolution.ServerUrl; | ||
| if (http is null || profiles is null || string.IsNullOrEmpty(serverUrl)) return null; | ||
| try { | ||
| var (client, status, _, _) = await http.ForWaitAsync(ct).ConfigureAwait(false); | ||
| using (client) { | ||
| if (status is not (AuthStatus.Ok or AuthStatus.NoAuthRequired)) return null; | ||
| var url = $"{serverUrl.TrimEnd('/')}/{ApiRoutes.ModelOptions}"; | ||
| using var response = await client.GetAsync(url, ct).ConfigureAwait(false); | ||
| if (!response.IsSuccessStatusCode) return null; | ||
| var raw = await response.Content.ReadFromJsonAsync( | ||
| RemoteModelsJsonContext.Default.DictionaryStringVendorModelOptionDtoArray, ct).ConfigureAwait(false); | ||
| return raw is null ? null : Map(raw); | ||
| } | ||
| } catch (HttpRequestException) { | ||
| return null; // offline / transport — an expected miss | ||
| } | ||
| }; | ||
|
|
||
| static IReadOnlyDictionary<string, IReadOnlyList<ModelChoice>> Map(Dictionary<string, VendorModelOptionDto[]> raw) => | ||
| raw.ToDictionary( | ||
| kv => kv.Key, | ||
| kv => (IReadOnlyList<ModelChoice>)[.. kv.Value.Select(o => new ModelChoice(o.Value, o.Label))], | ||
| StringComparer.OrdinalIgnoreCase); | ||
| } |
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
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,10 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Capacitor.Remote.Models; | ||
|
|
||
| /// One model the server offers for a vendor's launch dropdown, from GET api/agents/model-options. | ||
| /// Value is the exact model id carried on the launch wire; Label is the friendly display name. | ||
| public sealed record VendorModelOptionDto { | ||
| [JsonPropertyName("value")] public required string Value { get; init; } | ||
| [JsonPropertyName("label")] public required string Label { get; init; } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.