@0xkahi/pi-qol 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/assets/config.schema.json +10 -1
- package/package.json +1 -1
- package/src/codemap.md +25 -16
- package/src/extensions/auto-session-name/codemap.md +17 -12
- package/src/extensions/codemap.md +166 -13
- package/src/extensions/context-view/codemap.md +201 -16
- package/src/extensions/context-view/index.ts +7 -1
- package/src/extensions/context-view/ui/codemap.md +82 -0
- package/src/extensions/context-view/ui/context-view-dialog.ts +37 -51
- package/src/extensions/context-view/ui/injections-model.ts +2 -141
- package/src/extensions/context-view/ui/injections-view.ts +63 -195
- package/src/extensions/context-view/ui/usage-view.ts +68 -189
- package/src/extensions/model-select/codemap.md +34 -17
- package/src/extensions/model-select/index.ts +6 -13
- package/src/extensions/model-select/model-select-dialog.ts +85 -324
- package/src/extensions/model-select/types.ts +2 -2
- package/src/libs/codemap.md +8 -1
- package/src/libs/modal/README.md +223 -0
- package/src/libs/modal/codemap.md +35 -0
- package/src/libs/modal/index.ts +30 -0
- package/src/libs/modal/list-navigator.ts +167 -0
- package/src/libs/modal/modal-dialog.ts +297 -0
- package/src/libs/modal/navigation/pi-scheme.ts +33 -0
- package/src/libs/modal/navigation/vim-scheme.ts +56 -0
- package/src/libs/modal/presenter.ts +38 -0
- package/src/libs/modal/preview-layer.ts +87 -0
- package/src/libs/modal/render-cache.ts +33 -0
- package/src/libs/modal/tab-strip.ts +87 -0
- package/src/libs/modal/tabs/list-tab.ts +159 -0
- package/src/{extensions/context-view/ui/layout.ts → libs/modal/text.ts} +15 -15
- package/src/libs/modal/types.ts +70 -0
- package/src/schemas/codemap.md +101 -18
- package/src/schemas/context-view.config.schema.ts +5 -0
- package/src/extensions/context-view/ui/navigation.ts +0 -37
package/README.md
CHANGED
|
@@ -110,7 +110,8 @@ them (project overrides global):
|
|
|
110
110
|
"hide_tabs": { "groups": false, "search": false }
|
|
111
111
|
},
|
|
112
112
|
"context_view": {
|
|
113
|
-
"enabled": true
|
|
113
|
+
"enabled": true,
|
|
114
|
+
"layout": "inline"
|
|
114
115
|
},
|
|
115
116
|
"custom_footer": {
|
|
116
117
|
"enabled": true,
|
|
@@ -174,10 +175,11 @@ Duplicate favourite declarations remain first-entry-wins.
|
|
|
174
175
|
| Key | Type | Default | Description |
|
|
175
176
|
| --------- | --------- | ------- | ----------- |
|
|
176
177
|
| `enabled` | `boolean` | `false` | Enable the Context View capture and interface. |
|
|
178
|
+
| `layout` | `enum` | `inline` | Context View presentation: `inline` or centered `overlay`. |
|
|
177
179
|
|
|
178
180
|
### Context View
|
|
179
181
|
|
|
180
|
-
Context View is a fork of Dmitry Makarov's [pi-context-view](https://github.com/dimk90/pi-context-view), adapted into one unified tabbed interface with Vim-friendly keybindings. Enable it, then run `/context-view` with no arguments. The Usage tab opens first; Tab and Shift+Tab switch between Usage and Injections. Use `j`/`k` or arrows to move, `Ctrl+u`/`Ctrl+d` to move by a page, `gg`/`G` to jump to the beginning/end, Enter to preview, and Esc or `q` to return or close. Other extensions can open the same view by emitting `pi.vimKeys.event:pi-qol.context_view`.
|
|
182
|
+
Context View is a fork of Dmitry Makarov's [pi-context-view](https://github.com/dimk90/pi-context-view), adapted into one unified tabbed interface with Vim-friendly keybindings. Enable it, then run `/context-view` with no arguments. The Usage tab opens first; Tab and Shift+Tab switch between Usage and Injections. Use `j`/`k` or arrows to move, `Ctrl+u`/`Ctrl+d` to move by a page, `gg`/`G` to jump to the beginning/end, Enter to preview, and Esc or `q` to return or close. By default it opens inline; set `context_view.layout` to `overlay` for a centered overlay. Other extensions can open the same view by emitting `pi.vimKeys.event:pi-qol.context_view`.
|
|
181
183
|
|
|
182
184
|
#### `custom_footer`
|
|
183
185
|
|
|
@@ -257,13 +257,22 @@
|
|
|
257
257
|
},
|
|
258
258
|
"context_view": {
|
|
259
259
|
"default": {
|
|
260
|
-
"enabled": false
|
|
260
|
+
"enabled": false,
|
|
261
|
+
"layout": "inline"
|
|
261
262
|
},
|
|
262
263
|
"type": "object",
|
|
263
264
|
"properties": {
|
|
264
265
|
"enabled": {
|
|
265
266
|
"default": false,
|
|
266
267
|
"type": "boolean"
|
|
268
|
+
},
|
|
269
|
+
"layout": {
|
|
270
|
+
"default": "inline",
|
|
271
|
+
"type": "string",
|
|
272
|
+
"enum": [
|
|
273
|
+
"inline",
|
|
274
|
+
"overlay"
|
|
275
|
+
]
|
|
267
276
|
}
|
|
268
277
|
}
|
|
269
278
|
}
|
package/package.json
CHANGED
package/src/codemap.md
CHANGED
|
@@ -9,31 +9,32 @@ It coordinates four feature modules:
|
|
|
9
9
|
- **auto_session_name**: Generates and applies a concise session title from the user's first message before the agent runs.
|
|
10
10
|
- **model_select**: Registers a `/select-model` command (plus a cross-extension event) for picking or searching available models with favourites, ordered custom groups, provider filtering, and an inline or overlay layout.
|
|
11
11
|
- **custom_footer**: Replaces the default footer with a richer status bar showing cwd/branch/session, token/cost/cache stats, context-window usage, subscription-rate usage, and the current model/thinking level.
|
|
12
|
-
- **context_view**: Lazily captures the initial provider context
|
|
12
|
+
- **context_view**: Lazily captures and inspects the initial provider context, then opens a bounded half-height Usage/Injections interface through `/context-view` or `pi.vimKeys.event:pi-qol.context_view`, with configurable inline or centered overlay presentation.
|
|
13
13
|
|
|
14
|
-
Shared concerns live here too: extension configuration loading, path resolution, model/auth resolution,
|
|
14
|
+
Shared concerns live here too: extension configuration loading, path resolution, model/auth resolution, Zod schemas for every feature, and shared libraries under `libs/` — `libs/subscription-usage/` (provider usage facade) and `libs/modal/` (the self-contained modal dialog shell and presenter used by `model_select` and `context_view`).
|
|
15
15
|
|
|
16
16
|
## Design Patterns
|
|
17
17
|
|
|
18
|
-
- **Plugin / Extension registration**: `index.ts` is a thin bootstrap that
|
|
19
|
-
- **Event-driven lifecycle**: Features hook into `session_start`, `session_shutdown`,
|
|
18
|
+
- **Plugin / Extension registration**: `index.ts` is a thin bootstrap that creates a single `ConfigLoader` and calls each feature's `register*` factory. `registerContextView`, `registerCustomFooter`, and `registerModelSelect` are lazy-activated once per process when a `session_start` arrives with the feature enabled; `registerAutoSessionName` attaches its event listeners immediately.
|
|
19
|
+
- **Event-driven lifecycle**: Features hook into `session_start`, `session_shutdown`, `before_agent_start`, `input`, `turn_start`, `message_start`, `message_end`, `context`, `agent_settled`, and cross-extension events. They gate behaviour behind config flags and store transient state in closure variables (`lastSessionStartReason`, `titleController`, `installed`, `latestCtx`, `registered`, probe state, capture state).
|
|
20
20
|
- **Configuration layering**: `ConfigLoader` merges a built-in default with a global JSON config and an optional project-level config, validated and typed with Zod. Only trusted projects load project-level overrides.
|
|
21
21
|
- **Strategy pattern for subscription usage**: `SubscriptionUsageManager` dispatches to provider-specific `SubscriptionUsageStrategy` implementations (`AnthropicOauthUsageStrategy`, `OpenAiCodexUsageStrategy`) behind a common `SubscriptionUsageApi` interface.
|
|
22
|
-
- **Component-based TUI**: `CustomFooterComponent` and `
|
|
22
|
+
- **Component-based TUI**: `CustomFooterComponent`, `ModelSelectDialog`, and `ContextViewDialog` implement the host TUI's `Component`/`Focusable` interfaces, render to string arrays, and rely on the host for input routing and re-rendering. Both dialogs are thin configurations of the shared `libs/modal/` `ModalDialog` shell (tab strategies, pluggable navigation schemes, per-tab preview layers).
|
|
23
23
|
- **Abortable async work**: `auto_session_name` tracks an `AbortController` across `before_agent_start` invocations so stale title requests are cancelled before a new one starts.
|
|
24
|
+
- **Synthetic-probe capture**: `context_view` uses `InitialCaptureState` to freeze the first eligible provider context and `SilentProbeState` to emit a single empty probe when the user opens the view before any real turn has occurred.
|
|
24
25
|
- **Utility classes as pure helpers**: `PathUtil`, `ModelResolver`, `ModelFormatter`, `AutoSessionNameGuard`, `AutoSessionNameTitleGenerator` are stateless or request-scoped helpers.
|
|
25
26
|
|
|
26
27
|
## Data & Control Flow
|
|
27
28
|
|
|
28
29
|
1. **Bootstrap** (`index.ts`)
|
|
29
|
-
- Creates `ConfigLoader`.
|
|
30
|
+
- Creates a singleton `ConfigLoader`.
|
|
30
31
|
- On every `session_start`, calls `config.initializeConfig(ctx)`; surfaces validation errors via `ctx.ui.notify`.
|
|
31
|
-
-
|
|
32
|
+
- Calls `registerAutoSessionName`, `registerModelSelect`, `registerCustomFooter`, and `registerContextView`, each receiving the shared loader.
|
|
32
33
|
|
|
33
34
|
2. **Config loading** (`config-loader.ts`, `utils/path.util.ts`, `schemas/*.schema.ts`)
|
|
34
35
|
- Defaults → global `~/.pi/extensions/pi-qol/config.json` → project `.pi/extensions/pi-qol/config.json` (if trusted).
|
|
35
36
|
- Each partial config is shallow-merged per top-level key and re-validated by `ConfigSchema`.
|
|
36
|
-
- `PathUtil` owns the config/auth file path conventions.
|
|
37
|
+
- `PathUtil` owns the config/auth file path conventions and shell-style variable expansion.
|
|
37
38
|
|
|
38
39
|
3. **auto_session_name** (`extensions/auto-session-name/`)
|
|
39
40
|
- Captures `session_start.reason`.
|
|
@@ -42,20 +43,28 @@ Shared concerns live here too: extension configuration loading, path resolution,
|
|
|
42
43
|
- `AutoSessionNameTitleGenerator` builds a title-generation context, calls `completeSimple` from `pi-ai`, strips thinking tags/quotes, truncates to `MAX_TITLE_LENGTH`, and applies it via `pi.setSessionName`.
|
|
43
44
|
|
|
44
45
|
4. **model_select** (`extensions/model-select/`)
|
|
45
|
-
-
|
|
46
|
-
- `/select-model [args]` handler refreshes the registry, attempts an exact provider/model match, otherwise opens a custom TUI dialog with Favourites, group, and Search tabs.
|
|
47
|
-
- `ModelSelectDialog`
|
|
46
|
+
- Lazy-activates once on `session_start` when enabled.
|
|
47
|
+
- `/select-model [args]` handler waits for the session to be idle, refreshes the registry, attempts an exact provider/model match, otherwise opens a custom TUI dialog with Favourites, group, and Search tabs.
|
|
48
|
+
- `ModelSelectDialog` configures a shared `ModalDialog` with one `ListTab` per section (fuzzy filtering via the shell's shared filter input, wrap-around selection) and returns the chosen model to `pi.setModel` (optionally setting the configured default reasoning level).
|
|
48
49
|
- `PI_VIM_KEY_EVENT_ID` allows other extensions to trigger the same picker.
|
|
49
50
|
|
|
50
|
-
5. **
|
|
51
|
+
5. **context_view** (`extensions/context-view/`)
|
|
52
|
+
- Lazy-activates once on `session_start` when enabled; restores previously persisted probe message identities from the session manager.
|
|
53
|
+
- Owns `before_agent_start` prompt options; freezes the first eligible provider context snapshot on the `context` event.
|
|
54
|
+
- `input` observes extension empty inputs, `message_start`/`message_end` records probe identities, `turn_start` aborts the current probe, and `agent_settled`/`session_shutdown` settle the probe attempt.
|
|
55
|
+
- `/context-view` and the Vim event handler both call `prepareContextViewData`, which triggers a silent authenticated probe when no snapshot exists yet, then opens the `ContextViewDialog` through the shared modal presenter using the configured inline or overlay layout.
|
|
56
|
+
|
|
57
|
+
6. **custom_footer** (`extensions/custom-footer/`)
|
|
51
58
|
- On first enabled `session_start`, installs a footer component via `ctx.ui.setFooter`.
|
|
52
59
|
- Each render reads live data from `ExtensionContext` (`sessionManager`, `modelRegistry`, `model`, `getContextUsage`, `footerData`), calculates session totals, formats tokens/cost/context usage, and optionally fetches subscription usage through cached strategies.
|
|
53
60
|
|
|
54
61
|
## Integration Points
|
|
55
62
|
|
|
56
|
-
- **@earendil-works/pi-coding-agent**: Provides `ExtensionAPI`, `ExtensionContext`, `ExtensionCommandContext`, events (`session_start`, `session_shutdown`, `before_agent_start`), command registration (`pi.registerCommand`), UI notifications (`ctx.ui.notify`), footer injection (`ctx.ui.setFooter`), custom dialogs (`ctx.ui.custom`), session naming (`pi.setSessionName`/`getSessionName`),
|
|
57
|
-
- **@earendil-works/pi-ai**: Used for `completeSimple`, `Model<Api>`, `modelsAreEqual`, and model-metadata types during title generation and model selection.
|
|
58
|
-
- **@earendil-works/pi-tui**: Provides the `Component`/`Focusable` contracts, `TUI`, `Theme`, `Input`, and text-width helpers (`truncateToWidth`, `visibleWidth`, `fuzzyFilter`) used by the footer and
|
|
63
|
+
- **@earendil-works/pi-coding-agent**: Provides `ExtensionAPI`, `ExtensionContext`, `ExtensionCommandContext`, events (`session_start`, `session_shutdown`, `before_agent_start`, `input`, `turn_start`, `message_start`, `message_end`, `context`, `agent_settled`), command registration (`pi.registerCommand`), UI notifications (`ctx.ui.notify`), footer injection (`ctx.ui.setFooter`), custom dialogs (`ctx.ui.custom`), session naming (`pi.setSessionName`/`getSessionName`), model selection (`pi.setModel`/`getThinkingLevel`), session management (`ctx.sessionManager`), and tool registries (`pi.getAllTools`/`pi.getActiveTools`).
|
|
64
|
+
- **@earendil-works/pi-ai**: Used for `completeSimple`, `Model<Api>`, `modelsAreEqual`, `getSupportedThinkingLevels`, and model-metadata types during title generation and model selection.
|
|
65
|
+
- **@earendil-works/pi-tui**: Provides the `Component`/`Focusable` contracts, `TUI`, `Theme`, `Input`, `KeybindingsManager`, and text-width helpers (`truncateToWidth`, `visibleWidth`, `fuzzyFilter`) used by the footer, model picker, and context view.
|
|
59
66
|
- **Filesystem**: Reads global and project JSON configs and `~/.pi/auth.json` for OAuth tokens.
|
|
60
67
|
- **External provider APIs**: `SubscriptionUsageApi` and strategies call Anthropic/OpenAI Codex usage endpoints when OAuth auth is present.
|
|
61
|
-
- **Cross-extension events**: `piVimKeyEventId` from `constants.ts`
|
|
68
|
+
- **Cross-extension events**: `piVimKeyEventId` from `constants.ts` generates event ids consumed by `model_select` (`pi-qol.model_select`) and `context_view` (`pi-qol.context_view`).
|
|
69
|
+
|
|
70
|
+
(End of file)
|
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
## Responsibility
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Generate and apply a short, descriptive name for a Pi session based on the user's first user turn. The extension runs once per session, only on the user's first turn, and only when no session name has already been set and the session is not a child/fork. Generated titles are applied through the normal Pi session name API (`setSessionName`) so they appear in the UI and persist with the session.
|
|
6
6
|
|
|
7
7
|
## Design Patterns
|
|
8
8
|
|
|
9
9
|
- **Event-driven extension registration**: `registerAutoSessionName` attaches handlers to Pi lifecycle events (`session_start`, `before_agent_start`, `session_shutdown`) and coordinates state across them.
|
|
10
|
-
- **Guard object**: `AutoSessionNameGuard` isolates all preconditions (
|
|
10
|
+
- **Guard object**: `AutoSessionNameGuard` isolates all preconditions (enabled check is kept in the caller) so the main flow reads as a simple chain of boolean decisions.
|
|
11
11
|
- **Static command object**: `AutoSessionNameTitleGenerator` encapsulates the entire LLM-backed title generation as a single static entry point with pure helper methods for extraction, cleaning, and truncation.
|
|
12
12
|
- **Prompt-as-data**: `prompt.ts` builds a `Context` value object containing a detailed system prompt and a single user message; no prompt logic leaks into the generator.
|
|
13
|
-
- **Dependency injection by structural typing**: All collaborators are accepted as `Pick<...>` or local interfaces, making the extension easy to unit test and minimizing coupling to the full ExtensionAPI surface.
|
|
14
|
-
- **Cancellation token**: A module-level `AbortController` is created per title request
|
|
13
|
+
- **Dependency injection by structural typing**: All collaborators are accepted as `Pick<...>` or local interfaces, and the LLM call is optionally parameterized by `completeFn`, making the extension easy to unit test and minimizing coupling to the full ExtensionAPI surface.
|
|
14
|
+
- **Cancellation token**: A module-level `AbortController` is created per title request. It is aborted before a newer request starts, on `session_shutdown`, and propagated into the LLM completion so stale completions cannot overwrite the session name.
|
|
15
15
|
- **Defensive post-condition check**: After applying the title, the code compares `pi.getSessionName()` with the generated text and warns if they diverge.
|
|
16
16
|
|
|
17
17
|
## Data & Control Flow
|
|
@@ -23,12 +23,13 @@ Automatically generate and apply a short, descriptive name for a Pi session base
|
|
|
23
23
|
- The session is not a child/fork (`startReason !== 'fork'` and no `parentSession` header).
|
|
24
24
|
- This is the user's first turn (zero prior user messages in the branch).
|
|
25
25
|
- The current user prompt is non-empty.
|
|
26
|
-
3. **
|
|
27
|
-
4. **
|
|
28
|
-
5. **Title
|
|
29
|
-
6. **
|
|
30
|
-
7. **
|
|
31
|
-
8. **
|
|
26
|
+
3. **Cancellation of stale request**: Any existing `titleController` is aborted and replaced with a fresh `AbortController` for this request.
|
|
27
|
+
4. **Model resolution**: `ModelResolver` resolves the configured title model (or the default fallback) into a concrete `ResolvedModel` (model, API key, headers, reasoning preference). Errors are surfaced via `ctx.ui.notify`.
|
|
28
|
+
5. **Title generation**: `AutoSessionNameTitleGenerator.generateAndApplyTitle` sends a single-turn completion to the LLM using the context from `buildTitleContext`. The call carries `maxRetries`, `maxTokens`, and the abort signal.
|
|
29
|
+
6. **Title cleaning**: The raw response has `<think>` blocks stripped, is reduced to the first non-empty line, has surrounding quotes removed, and is truncated to `MAX_TITLE_LENGTH`.
|
|
30
|
+
7. **Application**: The cleaned title is applied via `pi.setSessionName`. If application fails or returns a different value, a warning is shown.
|
|
31
|
+
8. **Feedback**: Success or failure is reported through `ctx.ui.notify`.
|
|
32
|
+
9. **Cancellation/reset**: `session_shutdown` aborts any pending request and clears the controller.
|
|
32
33
|
|
|
33
34
|
## Integration Points
|
|
34
35
|
|
|
@@ -36,13 +37,17 @@ Automatically generate and apply a short, descriptive name for a Pi session base
|
|
|
36
37
|
- Events: `session_start`, `session_shutdown`, `before_agent_start`.
|
|
37
38
|
- Methods: `getSessionName`, `setSessionName`.
|
|
38
39
|
- **Pi Context/session manager** (`ctx.sessionManager`):
|
|
39
|
-
- `getBranch()` and `getHeader()` are used to detect first user turn and child sessions.
|
|
40
|
+
- `getBranch()` and `getHeader()` are used to detect the first user turn and child sessions.
|
|
40
41
|
- **ConfigLoader** (`../../config-loader`):
|
|
41
42
|
- `isEnabled('auto_session_name')` toggles the feature.
|
|
42
43
|
- `getAutoSessionName()` supplies the optional per-feature model override.
|
|
43
44
|
- **ModelResolver** (`../../utils/model-resolver.util`):
|
|
44
45
|
- Resolves the configured model string into a full `ResolvedModel` usable by `pi-ai`.
|
|
45
|
-
- **pi-ai** (`@earendil-works/pi-ai`):
|
|
46
|
+
- **pi-ai compat** (`@earendil-works/pi-ai/compat`):
|
|
46
47
|
- `completeSimple` performs the actual LLM completion.
|
|
47
48
|
- **Pi UI notifications** (`ctx.ui.notify`):
|
|
48
49
|
- All user-facing status, warnings, and errors are emitted through the Pi notification channel.
|
|
50
|
+
- **Web/Node `AbortController`**:
|
|
51
|
+
- Used to cancel in-flight LLM completion requests.
|
|
52
|
+
|
|
53
|
+
(End of file)
|
|
@@ -7,19 +7,169 @@ This folder contains the feature-level extension modules for `pi-qol`. Each chil
|
|
|
7
7
|
Current features:
|
|
8
8
|
|
|
9
9
|
- `auto-session-name` – automatically generates and applies a concise session title from the user’s first message.
|
|
10
|
-
- `model-select` – provides an interactive `/select-model` command (and cross-extension activation hook) for choosing models from the registry with permanent Favourites, ordered custom groups,
|
|
10
|
+
- `model-select` – provides an interactive `/select-model` command (and cross-extension activation hook) for choosing models from the registry with permanent Favourites, ordered custom groups, fuzzy search, provider filtering, and inline or overlay layout.
|
|
11
|
+
- `context-view` – captures model context contributions and displays Usage/Injections in a bounded half-height tabbed interface with configurable inline or overlay presentation, opened by command or Vim event.
|
|
11
12
|
- `custom-footer` – replaces the TUI footer with a richer status bar showing cwd, git branch, session name, model, token/cost usage, context-window usage, and subscription usage.
|
|
12
|
-
- `context-view` – captures initial context contributions and displays Usage/Injections in a bounded half-height inline tabbed interface with Vim navigation.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
---
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
## Extension Modules
|
|
17
|
+
|
|
18
|
+
### 1. `auto-session-name`
|
|
19
|
+
|
|
20
|
+
#### Context view
|
|
21
|
+
|
|
22
|
+
`registerAutoSessionName` keeps a small module-level state: `lastSessionStartReason` (the `reason` from the latest `session_start`) and `titleController` (the current `AbortController` for the in-flight title generation). This state is intentionally minimal because the feature is single-shot per session.
|
|
23
|
+
|
|
24
|
+
#### Event / lifecycle registration
|
|
25
|
+
|
|
26
|
+
The registration function wires three `ExtensionAPI` events:
|
|
27
|
+
|
|
28
|
+
- `session_start` – stores the session start reason for later child-session detection.
|
|
29
|
+
- `session_shutdown` – aborts and clears any pending title request.
|
|
30
|
+
- `before_agent_start` – performs the guard checks, aborts the previous request, and starts the async title generation.
|
|
31
|
+
|
|
32
|
+
#### Data & control flow
|
|
33
|
+
|
|
34
|
+
1. On `before_agent_start` the handler short-circuits unless:
|
|
35
|
+
- `auto_session_name` is enabled.
|
|
36
|
+
- No session name is currently set.
|
|
37
|
+
- The session is not a child/fork session (using `lastSessionStartReason` and `ctx.sessionManager`).
|
|
38
|
+
- This is the user’s first turn.
|
|
39
|
+
- The current prompt contains non-empty text.
|
|
40
|
+
2. The configured model is resolved via `ModelResolver` from the per-feature config.
|
|
41
|
+
3. `AutoSessionNameTitleGenerator.generateAndApplyTitle` builds a single-turn prompt, calls `completeSimple`, strips `<think>` blocks, cleans and truncates the result, and applies it via `pi.setSessionName`.
|
|
42
|
+
4. A post-condition compares `pi.getSessionName()` with the generated title; a warning is shown if they diverge.
|
|
43
|
+
5. Success or any failure is reported through `ctx.ui.notify`.
|
|
44
|
+
|
|
45
|
+
#### Integration points
|
|
46
|
+
|
|
47
|
+
- **`@earendil-works/pi-coding-agent`**: `ExtensionAPI` events (`session_start`, `session_shutdown`, `before_agent_start`), `getSessionName`, `setSessionName`, and `ctx.ui.notify`.
|
|
48
|
+
- **`@earendil-works/pi-ai`**: `completeSimple` for the LLM title completion.
|
|
49
|
+
- **`../../config-loader`**: `isEnabled('auto_session_name')` and `getAutoSessionName()` for the feature toggle and model override.
|
|
50
|
+
- **`../../utils/model-resolver.util`**: `ModelResolver` resolves provider/model/auth configuration.
|
|
51
|
+
- **`ctx.sessionManager`**: used by `AutoSessionNameGuard` to detect first user turns and parent sessions.
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
### 2. `model-select`
|
|
56
|
+
|
|
57
|
+
#### Context view
|
|
58
|
+
|
|
59
|
+
`registerModelSelect` exposes `applySelectedModel` and `showModelSelector` as the public action surface. The registration itself is lazy: `modelSelectRegistered` ensures `activateModelSelect` is only run once per process. `activateModelSelect` caches the most recent `ExtensionContext` in `latestCtx` so the cross-extension event bus can open the picker even when no command context is available.
|
|
60
|
+
|
|
61
|
+
#### Event / lifecycle registration
|
|
62
|
+
|
|
63
|
+
- `session_start` – the outer `registerModelSelect` listener checks the feature flag and lazily activates the module once.
|
|
64
|
+
- Inside `activateModelSelect`:
|
|
65
|
+
- `session_start` refreshes `latestCtx`.
|
|
66
|
+
- `pi.registerCommand(COMMAND_NAME)` registers `/select-model` with the interactive handler.
|
|
67
|
+
- `pi.events.on(PI_VIM_KEY_EVENT_ID)` listens for cross-extension activation (e.g., from `pi-vim-keys`).
|
|
68
|
+
|
|
69
|
+
#### Data & control flow
|
|
70
|
+
|
|
71
|
+
1. The command handler records the latest context, checks the feature flag, and calls `showModelSelector`.
|
|
72
|
+
2. `showModelSelector` waits for idle when available, refreshes `ctx.modelRegistry`, and attempts an exact provider/model match through `findExactModel`.
|
|
73
|
+
3. If an exact match succeeds, the model is applied immediately via `applySelectedModel` and the dialog is skipped.
|
|
74
|
+
4. Otherwise, `buildModelLists` prepares Favourites, ordered group subsets, and Search items. Provider filtering is applied only to Search, and favourite auth/registry warnings are collected.
|
|
75
|
+
5. The shared `presentModal` helper creates a `ModelSelectDialog` configured with the current model, lists, tab visibility, warnings, initial search, and the resolved frame for the configured layout.
|
|
76
|
+
6. The dialog shares a single query across visible tabs, preserves per-tab selection, cycles tabs, and renders either inline or as a centered overlay at 85% width.
|
|
77
|
+
7. On selection, `applySelectedModel` calls `pi.setModel` and, if supported, sets the configured default reasoning level via `pi.setThinkingLevel`; missing auth is reported as an error.
|
|
78
|
+
|
|
79
|
+
#### Integration points
|
|
80
|
+
|
|
81
|
+
- **`@earendil-works/pi-coding-agent`**: `ExtensionAPI` command registration, event subscription, `setModel`, `setThinkingLevel`, and `getThinkingLevel`; `ExtensionContext` / `ExtensionCommandContext` for `modelRegistry`, `ui`, `hasUI`, `model`, and `waitForIdle`.
|
|
82
|
+
- **`@earendil-works/pi-ai`**: `Model<Api>` type, `getSupportedThinkingLevels`, and `modelsAreEqual` helpers.
|
|
83
|
+
- **`@earendil-works/pi-tui`**: `Component`, `Focusable`, `Input`, `fuzzyFilter`, `matchesKey`, `Theme`, and width/truncation helpers.
|
|
84
|
+
- **`../../config-loader`**: `isEnabled('model_select')` and the full `model_select` configuration.
|
|
85
|
+
- **`../../constants`**: `piVimKeyEventId` generates the shared event id used by the Vim-keys integration.
|
|
86
|
+
- **Cross-extension consumers**: `pi.events.emit(PI_VIM_KEY_EVENT_ID)` opens the picker without invoking the slash command.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
### 3. `context-view`
|
|
91
|
+
|
|
92
|
+
#### Context view
|
|
93
|
+
|
|
94
|
+
`registerContextView` is disabled by default and only activates once an enabled session starts. The activation keeps `InitialCaptureState` for the first provider snapshot, `SilentProbeState` for synthetic-probe management, and `latestCtx` for the event-bus handler. The module is intentionally careful to persist only synthetic probe identities (role/timestamp) and never real prompt, tool, or message content.
|
|
95
|
+
|
|
96
|
+
#### Event / lifecycle registration
|
|
97
|
+
|
|
98
|
+
`activateContextView` registers a dense set of lifecycle hooks:
|
|
99
|
+
|
|
100
|
+
- `session_start` – refreshes `latestCtx` and restores persisted probe identities.
|
|
101
|
+
- `input` – observes owned user inputs.
|
|
102
|
+
- `before_agent_start` – begins a probe run and prepares capture from system prompt options.
|
|
103
|
+
- `turn_start` – aborts the current probe run if needed.
|
|
104
|
+
- `message_start` / `message_end` – records and sanitizes assistant messages.
|
|
105
|
+
- `context` – filters provider messages, freezes the first eligible snapshot, and can optionally replace the outgoing messages.
|
|
106
|
+
- `agent_settled` – settles the probe and persists new identities.
|
|
107
|
+
- `session_shutdown` – persists identities and fails the probe.
|
|
108
|
+
- `pi.registerCommand(COMMAND_NAME)` registers `/context-view`.
|
|
109
|
+
- `pi.events.on(PI_VIM_KEY_EVENT_ID)` opens the view from a Vim event.
|
|
110
|
+
|
|
111
|
+
#### Data & control flow
|
|
112
|
+
|
|
113
|
+
1. On an enabled session start, the module is installed and any persisted probe identities are restored.
|
|
114
|
+
2. `before_agent_start` prepares the initial capture; the `context` event freezes the first eligible provider snapshot.
|
|
115
|
+
3. If the view is opened before capture completes, a silent probe runs; synthetic messages are filtered, sanitized, and persisted by identity only.
|
|
116
|
+
4. `prepareContextViewData` combines the snapshot, current native prompt/tool data, filtered session messages, and reported usage.
|
|
117
|
+
5. `ContextViewDialog` opens as a Usage-first component bounded to 50% of the terminal height; the shared presenter selects inline or centered overlay mounting while retaining state for both tabs.
|
|
118
|
+
6. Shared navigation (`j`/`k`, arrows, `Ctrl+u`/`d`, `gg`/`G`) is routed through `ui/navigation.ts`.
|
|
119
|
+
|
|
120
|
+
#### Integration points
|
|
121
|
+
|
|
122
|
+
- **`@earendil-works/pi-coding-agent`**: `ExtensionAPI` events, `registerCommand`, `events` bus, `buildSessionContext`, and `ctx.sessionManager`.
|
|
123
|
+
- **`../../config-loader`**: `isEnabled('context_view')` gates registration and command handlers.
|
|
124
|
+
- **`../../constants`**: `piVimKeyEventId` generates the cross-extension event id.
|
|
125
|
+
- **`@earendil-works/pi-tui`**: `Component`, `Input`, and focus/routing helpers for the dialog and child views.
|
|
126
|
+
- **`ui/` and `capture.ts`**: module-local helpers classify, measure, and render context usage and injections.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
### 4. `custom-footer`
|
|
131
|
+
|
|
132
|
+
#### Context view
|
|
133
|
+
|
|
134
|
+
`registerCustomFooter` stores a single `installed` flag to prevent duplicate footer registration. The actual component is created per render by the factory passed to `ctx.ui.setFooter`, receiving live `tui`, `theme`, `footerData`, `ExtensionContext`, `ConfigLoader`, and a `getThinkingLevel` accessor.
|
|
135
|
+
|
|
136
|
+
#### Event / lifecycle registration
|
|
137
|
+
|
|
138
|
+
- `session_start` – the only registration event. When the feature is enabled and the footer is not yet installed, it calls `ctx.ui.setFooter` with a factory that creates a `CustomFooterComponent` for each render.
|
|
139
|
+
- No shutdown handler is required because the component implements `dispose` for its own cleanup.
|
|
140
|
+
|
|
141
|
+
#### Data & control flow
|
|
142
|
+
|
|
143
|
+
1. On `session_start`, `setFooter` installs the custom footer factory.
|
|
144
|
+
2. The TUI calls `CustomFooterComponent.render(width)` on every footer repaint.
|
|
145
|
+
3. If the feature is disabled at render time, the component calls `ctx.ui.setFooter(undefined)` once to restore the default footer and returns an empty array.
|
|
146
|
+
4. The component assembles up to three lines:
|
|
147
|
+
- `renderPathLine`: directory (with optional custom color), git branch, and session name.
|
|
148
|
+
- `renderStatsLine`: token totals, cache read/write, cache hit rate, estimated cost, context-window usage, subscription usage, and active model.
|
|
149
|
+
- Extension status line when statuses exist.
|
|
150
|
+
5. `token-stats.ts` aggregates assistant-message token counts and cache hit rates from `sessionManager.getEntries()`.
|
|
151
|
+
6. `SubscriptionUsageManager` caches OAuth usage with a TTL and coalesces in-flight requests; on update it calls `tui.requestRender()` to refresh the footer.
|
|
152
|
+
7. `FooterDataProvider` watches git branch changes and similarly requests re-renders; the callback is removed in `dispose`.
|
|
153
|
+
|
|
154
|
+
#### Integration points
|
|
155
|
+
|
|
156
|
+
- **`@earendil-works/pi-coding-agent`**: `ExtensionAPI` events, `getThinkingLevel`; `ExtensionContext` for `model`, `modelRegistry`, `sessionManager`, `getContextUsage`, and `ui.setFooter`.
|
|
157
|
+
- **`@earendil-works/pi-tui`**: `Component` interface, `truncateToWidth`, `visibleWidth`, and `TUI.requestRender`.
|
|
158
|
+
- **`../../config-loader`**: `isEnabled('custom_footer')` and full footer configuration (colors, icons, display toggles).
|
|
159
|
+
- **`../../libs/subscription-usage/`**: `SubscriptionUsageApi` and provider-specific strategies (Anthropic OAuth, OpenAI Codex) for subscription usage.
|
|
160
|
+
- **`@0xkahi/cli-dye`**: truecolor ANSI styling for directory, model, and subscription usage segments.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Cross-Cutting Design Patterns
|
|
165
|
+
|
|
166
|
+
- **Registration function pattern**: each module exports a single `register*` function that takes `ExtensionAPI` and a `{ config: ConfigLoader }` dependency object. `src/index.ts` calls all four registration functions after initializing configuration.
|
|
167
|
+
- **Event-driven lifecycle wiring**: registration is non-blocking; actual work is triggered by Pi runtime events (`session_start`, `session_shutdown`, `before_agent_start`, `input`, `turn_start`, `message_start`, `message_end`, `context`, `agent_settled`).
|
|
168
|
+
- **Lazy / once-only activation**: `model-select`, `context-view`, and `custom-footer` use guard flags (`modelSelectRegistered`, `registered`, `installed`) to ensure handlers or UI components are registered only once per process.
|
|
169
|
+
- **Command registration**: `model-select` and `context-view` register slash commands via `pi.registerCommand` and defer work to async handlers.
|
|
170
|
+
- **TUI component pattern**: `custom-footer` and the two dialogs construct Pi TUI components that render on every frame and participate in focus/input routing.
|
|
21
171
|
- **Guard classes**: `auto-session-name/guards.ts` isolates predicate logic (session name already set, child session, first user turn) for easy testing and readable early returns.
|
|
22
|
-
- **Pure helpers / formatters**: `model-formatter.ts`, `token-stats.ts`, `model-lists.ts`, and `
|
|
172
|
+
- **Pure helpers / formatters**: `model-formatter.ts`, `token-stats.ts`, `model-lists.ts`, `prompt.ts`, `measure.ts`, and `usage.ts` keep transformation and rendering logic free of side effects.
|
|
23
173
|
- **Cross-extension event bus**: `model-select` and `context-view` listen on dedicated `pi.events` channels so other extensions can open their interfaces without invoking slash commands directly.
|
|
24
174
|
- **Abortable async work**: `auto-session-name` keeps an `AbortController` and cancels in-flight title generation on `session_shutdown` or a new `before_agent_start`.
|
|
25
175
|
|
|
@@ -47,7 +197,7 @@ Current features:
|
|
|
47
197
|
4. **context-view flow**
|
|
48
198
|
- Lazily activates only when `context_view.enabled` is true at session start.
|
|
49
199
|
- Captures owned prompt/tool/message inputs, freezes the first provider-context snapshot, and filters exact synthetic-probe identities.
|
|
50
|
-
- `/context-view` and `pi.vimKeys.event:pi-qol.context_view` share data preparation and open a bounded half-height Usage-first inline
|
|
200
|
+
- `/context-view` and `pi.vimKeys.event:pi-qol.context_view` share data preparation and open a bounded half-height Usage-first interface through the shared presenter using the configured inline or overlay layout.
|
|
51
201
|
- The dialog retains Usage/Injections child state and routes Tab plus Vim navigation (`j/k`, `Ctrl+u/d`, `gg/G`).
|
|
52
202
|
|
|
53
203
|
5. **custom-footer flow**
|
|
@@ -58,10 +208,13 @@ Current features:
|
|
|
58
208
|
## Integration Points
|
|
59
209
|
|
|
60
210
|
- **`@earendil-works/pi-coding-agent`** – `ExtensionAPI`, `ExtensionContext`, `ExtensionCommandContext`, events, commands, session manager, and UI notifications.
|
|
61
|
-
- **`@earendil-works/pi-ai`** – model types (`Model`, `Api`), `completeSimple`, and `modelsAreEqual`.
|
|
211
|
+
- **`@earendil-works/pi-ai`** – model types (`Model`, `Api`), `completeSimple`, `getSupportedThinkingLevels`, and `modelsAreEqual`.
|
|
62
212
|
- **`@earendil-works/pi-tui`** – `Component`, `Focusable`, `Input`, `TUI`, `Theme`, `KeybindingsManager`, and width helpers.
|
|
63
|
-
- **`src/config-loader.ts`** – shared `ConfigLoader` used by all
|
|
213
|
+
- **`src/config-loader.ts`** – shared `ConfigLoader` used by all four registration functions for feature flags and section config.
|
|
64
214
|
- **`src/utils/model-resolver.util.ts`** – resolves provider/model/auth configuration for `auto-session-name`.
|
|
65
215
|
- **`src/libs/subscription-usage/`** – `SubscriptionUsageApi` and provider-specific strategies used by the footer to fetch OAuth subscription usage.
|
|
66
|
-
- **`src/constants.ts` / `piVimKeyEventId`** – generates the cross-extension event
|
|
216
|
+
- **`src/constants.ts` / `piVimKeyEventId`** – generates the cross-extension event ids consumed by `model-select` and `context-view`.
|
|
67
217
|
- **`src/index.ts`** – orchestrates config initialization and calls each `register*` function in a fixed order.
|
|
218
|
+
- **`@0xkahi/cli-dye`** – truecolor ANSI styling used by the custom footer.
|
|
219
|
+
|
|
220
|
+
(End of file)
|