@benvargas/pi-model-sort
v1.0.0
Published
Sort models in pi by last usage and start fresh sessions on the most recently used model
Maintainers
Readme
@benvargas/pi-model-sort
Sorts pi's model picker by last usage and starts fresh sessions on your most recently used model.
/modelpicker — both "Scope: all" and "Scope: scoped" views, including fuzzy-search results — is sorted by recency: current model first → most recently used → provider/id alphabetical- Ctrl+P / Ctrl+Shift+P scoped cycling follows last-used order (scoped models come from
enabledModelsor--models) - Fresh starts and
/newbegin on your most recently used model instead ofenabledModels[0]or the hardcoded provider default - Continued sessions (
pi -c,--session,/resume, forks) keep the model saved in the session file, and that restored model is recorded as last-used (fresh/newsessions and/reloadare excluded from that recording) - Remembers the thinking level you last used on each model and restores it on every switch, clamped to what each model supports
- No configuration needed — tracking starts on first use and degrades to the default alphabetical order with no history
Forked from monotykamary/pi-model-sort (MIT, v0.3.2). See
THIRD_PARTY_NOTICES.mdfor the full list of fork changes and attribution.
Install
pi install npm:@benvargas/pi-model-sortOr try without installing:
pi -e npm:@benvargas/pi-model-sortUsage
The extension works automatically — there are no commands to learn.
/model # Most recently used models appear at the top
Ctrl+P / Ctrl+Shift+P # Cycle through scoped models in last-used order
pi # Fresh starts use MRU
pi -c # Continuations keep the session's modelHow It Works
- Tracking uses pi's documented extension events:
/modelswitches (model_select) and thinking-level changes (thinking_level_select) are timestamped into~/.pi/agent/extensions/pi-model-sort.json. Continued sessions restore their model during construction without emittingmodel_select(pi 0.84.3), so the extension records the restored model atsession_startitself. - Sorting has no SDK hook, so the extension wraps (monkey-patches) internal methods:
ModelSelectorComponent.sortModels, its scoped loader andfilterModels, andAgentSession._cycleScopedModelfor scoped cycling. All original methods are preserved and restored on shutdown/reload; the patches survivemodelRegistry.refresh(). - The MRU startup override calls
pi.setModel()onsession_startfor fresh starts and/newonly. A continued session is detected by projecting its branch through pi's own context-message rules (message,custom_message, non-emptybranch_summary, andcompactionentries — exactly whatbuildSessionContext()counts) — pi seeds every new session withmodel_change+thinking_level_changeentries beforesession_startfires, so raw branch length cannot distinguish fresh from continued.
Known limitations on pi 0.84.x
--list-modelsoutput is not sorted: the CLI lists models before extensions load and re-sorts by provider/id internally./scoped-modelsand unscoped Ctrl+P cycling are not re-sorted: they readModelRuntimesnapshots directly, not the extension-facingModelRegistryfacade this extension wraps. (Scoped cycling — the common case withenabledModelsset — is sorted via the_cycleScopedModelwrapper.)
Configuration
Usage history lives in ~/.pi/agent/extensions/pi-model-sort.json:
{
"lastUsed": {
"provider/modelId": 1717000000000
},
"thinking": {
"provider/modelId": "high"
}
}No manual editing is needed. To clear usage history, delete the file and /reload.
Notes
- Ctrl+P cycling does not update last-used timestamps — doing so would create a sort feedback loop (each cycle step re-sorts the selected model to the top, making the cycle toggle forever between the top two). Manual selections and session restores still update it.
- Patches are coupled to pi internals; this package is maintained against current pi releases (peer requirement
>=0.84.0, the version it is tested with).
Uninstall
pi remove npm:@benvargas/pi-model-sort