Skip to content

TUI: auto-refresh empty model cache inline on /model - #175

Merged
FMXExpress merged 2 commits into
mainfrom
claude/tui-model-inline-refresh
Jun 7, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/tui-model-inline-refresh

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Stacks on PR #173 + PR #174. Replaces the "flash a hint and bail" path in OpenModelMenu with an inline auto-refresh: when /model is invoked and the cache is empty / missing, the TUI runs /v1/models itself and opens the picker as soon as the worker completes — no more drop-out-to-CLI ritual.

Before: type /model → "no cached models — run pasclaw 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

  • Delphi build (async). New TModelRefreshThread mirrors TRunToolLoopThread — calls DiscoverModels off the UI thread, signals via TEvent. New PollModelRefresh, called from the Run() loop next to PollLoopWorker, picks up the result on the main thread, writes $PASCLAW_HOME/cache/models/<provider>.json via SaveCachedModels, and raises the overlay through the same OpenModelOverlay helper 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 → FreeOnTerminate pattern the tool loop uses on shutdown. Mutual exclusion: a second /model while a refresh is in flight flashes "still fetching..." instead of stacking workers.
  • FPC build (sync). Line-based UI is already blocking on ReadLn, so the auto-refresh just inlines DiscoverModels directly. Prints fetching /models from <name>... and shows the populated list when the call returns.
  • Shared spec resolution. Extracted ResolveProviderSpecForName out of PasClaw.Cmd.Model into PasClaw.Providers.Models so the TUI and CLI walk one path through Kind normalisation (openai-compat → openai), Name-fallback when Kind is blank (PR Model discovery: live /v1/models lookup + cache + onboarding picker #171 Codex P2 contract), and Spec.DefaultBase when the operator hasn't overridden APIBase. Cmd.Model's RefreshOne now calls the extracted helper; the local FindProviderConfig / ResolveSpecForName are deleted.

Tests

  • model_discovery_tests adds TestResolveProviderSpecForName covering: missing config entry → actionable error, blank-Kind → Name fallback to a real catalog spec, blank APIBase → Spec.DefaultBase, explicit APIBase wins over default, openai-compat Kind normalises to openai.
  • make clean rebuild + make test — all suites green (smoke, hashline, toolview, server-tools, printer, utf8, schema-strip, markdown, json-utf8, model_discovery).

Test plan

  • make rebuilds cleanly on FPC
  • make test — all suites pass, including the new TestResolveProviderSpecForName
  • Delphi pasclaw tui — /model against a provider with no cache shows fetching flash, then opens the picker after ~1-3s
  • Delphi pasclaw tui — /model while a refresh is in flight flashes "still fetching..."
  • Delphi pasclaw tui — exit TUI while a refresh is in flight does not crash (bounded-wait cleanup)
  • FPC pasclaw tui — bare /model against an empty cache prints fetching... then the populated list
  • Refresh failure (bad API key, offline) flashes an actionable error and leaves the operator in the TUI

Stacking

Based on claude/tui-slash-model-delphi-fix (PR #174) which in turn is based on claude/tui-slash-model (PR #173). Targets main — merging this alone delivers PR #173 + PR #174 + this PR's contents.


Generated by Claude Code

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/pkg/tui/PasClaw.TUI.pas Outdated
end;

SaveCachedModels(Provider, R);
OpenModelOverlay(Provider, R);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants