Repository navigation
TUI: auto-refresh empty model cache inline on /model - #175
Conversation
Hitting /model when the cache is empty / missing used to flash "run pasclaw model refresh" and bail, forcing the operator to quit the TUI, run the CLI, and restart. Now the TUI just does it. Delphi build spawns TModelRefreshThread (same shape as TRunToolLoopThread) that calls DiscoverModels off the UI thread and signals via TEvent. PollModelRefresh, called from the Run() loop next to PollLoopWorker, picks up the result, writes the cache file, and raises the overlay. 15-second wall clock guards against a stuck HTTP. Destructor cleans up an in-flight refresh with the same bounded-wait-then-FreeOnTerminate pattern the tool loop uses. FPC build inlines a synchronous DiscoverModels call when the cache is empty — its line-based UI doesn't fight a render thread, so blocking on HTTP is the right shape there. Extracted ResolveProviderSpecForName (Cmd.Model's spec resolution walk, normalises Kind / Name-fallback / DefaultBase) into PasClaw.Providers.Models so the TUI auto-refresh and the CLI `pasclaw model refresh` share one path. Pinned its contract with a new test (TestResolveProviderSpecForName) in model_discovery_tests.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 448a1eed74
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| end; | ||
|
|
||
| SaveCachedModels(Provider, R); | ||
| OpenModelOverlay(Provider, R); |
There was a problem hiding this comment.
Keep /model refresh tied to its originating session
When /model starts from session A with an empty cache and the user switches to session B before the HTTP refresh returns, this completion path opens A's model list against the current UI state. The menu selection then flows through ApplyModelSelection, which writes the chosen id into the currently selected FSession.Meta.Model, so session B can be saved with a model from A's provider. Store the originating session/provider and either apply the selection to that session or discard/retry the result if the active session has changed.
Useful? React with 👍 / 👎.
When /model is invoked in session A (provider=openai) and the operator switches to session B (provider=anthropic) before the HTTP refresh returns, PollModelRefresh used to open A's roster against B's context. ApplyModelSelection on a pick would then write an openai model id into B's Meta.Model, so B silently ends up talking to anthropic with an openai id. Same shape as the FLoopSessionId fix on PR #122. Cache save is unconditional (the HTTP succeeded; the roster is authoritative regardless of focus) but the overlay only raises when the currently-active session still uses the refreshed provider. Provider-mismatch flashes a "switch to a session using <name> to pick" notice instead. Extracted the "active provider" lookup OpenModelMenu and PollModelRefresh now both rely on into a shared ActiveProviderName helper so they agree on the comparison.
Summary
Stacks on PR #173 + PR #174. Replaces the "flash a hint and bail" path in
OpenModelMenuwith an inline auto-refresh: when/modelis invoked and the cache is empty / missing, the TUI runs/v1/modelsitself and opens the picker as soon as the worker completes — no more drop-out-to-CLI ritual.Before: type
/model→ "no cached models — runpasclaw model refresh openai" →:q→ exit TUI →pasclaw model refresh openai→pasclaw tui→ type/model→ picker opens.After: type
/model→fetching models from OpenAI...(flash) → picker opens when the fetch returns.Design
TModelRefreshThreadmirrorsTRunToolLoopThread— callsDiscoverModelsoff the UI thread, signals viaTEvent. NewPollModelRefresh, called from theRun()loop next toPollLoopWorker, picks up the result on the main thread, writes$PASCLAW_HOME/cache/models/<provider>.jsonviaSaveCachedModels, and raises the overlay through the sameOpenModelOverlayhelper that the cache-hit path uses. 15-second wall clock catches stuck HTTP. Destructor cleans up an in-flight refresh with the same bounded-wait →FreeOnTerminatepattern the tool loop uses on shutdown. Mutual exclusion: a second/modelwhile a refresh is in flight flashes "still fetching..." instead of stacking workers.ReadLn, so the auto-refresh just inlinesDiscoverModelsdirectly. Printsfetching /models from <name>...and shows the populated list when the call returns.ResolveProviderSpecForNameout ofPasClaw.Cmd.ModelintoPasClaw.Providers.Modelsso the TUI and CLI walk one path through Kind normalisation (openai-compat→openai), Name-fallback when Kind is blank (PR Model discovery: live/v1/modelslookup + cache + onboarding picker #171 Codex P2 contract), andSpec.DefaultBasewhen the operator hasn't overridden APIBase. Cmd.Model'sRefreshOnenow calls the extracted helper; the localFindProviderConfig/ResolveSpecForNameare deleted.Tests
model_discovery_testsaddsTestResolveProviderSpecForNamecovering: missing config entry → actionable error, blank-Kind → Name fallback to a real catalog spec, blank APIBase →Spec.DefaultBase, explicit APIBase wins over default,openai-compatKind normalises toopenai.makeclean rebuild +make test— all suites green (smoke, hashline, toolview, server-tools, printer, utf8, schema-strip, markdown, json-utf8, model_discovery).Test plan
makerebuilds cleanly on FPCmake test— all suites pass, including the newTestResolveProviderSpecForNamepasclaw tui—/modelagainst a provider with no cache shows fetching flash, then opens the picker after ~1-3spasclaw tui—/modelwhile a refresh is in flight flashes "still fetching..."pasclaw tui— exit TUI while a refresh is in flight does not crash (bounded-wait cleanup)pasclaw tui— bare/modelagainst an empty cache printsfetching...then the populated listStacking
Based on
claude/tui-slash-model-delphi-fix(PR #174) which in turn is based onclaude/tui-slash-model(PR #173). Targets main — merging this alone delivers PR #173 + PR #174 + this PR's contents.Generated by Claude Code