proper-base 0.4.0 → 0.5.1
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 +31 -5
- package/index.ts +92 -10
- package/package.json +4 -3
- package/src/autocomplete-details.ts +154 -21
- package/src/editor-mouse.ts +69 -0
- package/src/editor-navigation.ts +52 -1
- package/src/image-preview.ts +21 -4
- package/src/prompt-jump.ts +308 -54
- package/src/settings.ts +239 -0
- package/src/startup-defaults.ts +143 -0
- package/src/transcript-cleanup.ts +109 -9
package/README.md
CHANGED
|
@@ -20,6 +20,14 @@ and footer layout.
|
|
|
20
20
|
keep their names.
|
|
21
21
|
- `/clear` starts an empty session but restores the exact provider and model
|
|
22
22
|
selected in the previous one. No messages, name, or branch state carry over.
|
|
23
|
+
- The model you pick in `/model` and the level you pick in `/thinking` become
|
|
24
|
+
Pi's startup defaults, so the next session opens on them. Pi otherwise saves
|
|
25
|
+
those only when you press Ctrl+S in the picker, and because Pi re-derives the
|
|
26
|
+
thinking level from the saved default on every model switch, an unsaved level
|
|
27
|
+
is otherwise lost mid-session at the next `/model`, Ctrl+P, or `/clear`.
|
|
28
|
+
Switching to a model that cannot reach the current level records the level
|
|
29
|
+
you actually get. Resuming a session does not redefine the model. Set
|
|
30
|
+
`"stickyDefaults": false` in `~/.pi/agent/proper-base.json` to turn this off.
|
|
23
31
|
- Prompt-template expansions remain model-facing, while the transcript shows
|
|
24
32
|
the slash command you typed, such as `/implement-ready epic-1 4`.
|
|
25
33
|
- CLIProxyAPI `empty_stream` failures become normal retryable network errors, so
|
|
@@ -47,7 +55,9 @@ proper-base records eligible editor submissions on a best-effort basis, not
|
|
|
47
55
|
transformed Pi session messages. History uses an encoded key derived from the
|
|
48
56
|
current working directory.
|
|
49
57
|
|
|
50
|
-
- Up and Down recall prompts from previous sessions in the same project.
|
|
58
|
+
- Up and Down recall prompts from previous sessions in the same project. Up
|
|
59
|
+
recalls only from an empty prompt; with a draft it only moves the cursor,
|
|
60
|
+
so Home then Up can no longer replace what you typed.
|
|
51
61
|
- Ctrl+R starts case-sensitive reverse substring search. Press Ctrl+R again for
|
|
52
62
|
an older match, Backspace or Shift+Backspace to broaden the query, Enter to
|
|
53
63
|
submit, Esc to keep the match for editing, or Ctrl+G to restore the original
|
|
@@ -75,9 +85,18 @@ directory to forget all proper-base history.
|
|
|
75
85
|
and URLs.
|
|
76
86
|
- `/model ` results sort by displayed model ID in descending numeric-aware
|
|
77
87
|
order. Typed terms must all match when strict matches exist.
|
|
78
|
-
-
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
- `/model` takes an optional thinking level after the model name, as in
|
|
89
|
+
`/model anthropic/claude-opus-4 high`. Once the model name carries a
|
|
90
|
+
provider slash, the next word completes against Pi's thinking levels; a
|
|
91
|
+
word naming no level keeps searching models.
|
|
92
|
+
- Tab-completing a model name submits nothing. It leaves the name, a
|
|
93
|
+
trailing space, and an open level menu led by the level already in
|
|
94
|
+
effect, so you can type a level straight away or press Enter to keep the
|
|
95
|
+
current one.
|
|
96
|
+
- Tab or Enter on a level completion switches immediately: the level is
|
|
97
|
+
the last argument, so `/model gp<Tab>lo<Tab>` picks the model and `low`
|
|
98
|
+
without a further Enter. Either key switches only when the complete
|
|
99
|
+
prompt is a single-line `/model ...` command.
|
|
81
100
|
|
|
82
101
|
### Fullscreen navigation and selection
|
|
83
102
|
|
|
@@ -109,6 +128,13 @@ to the bottom. While you are scrolled up, a dimmer `position/total` reading sits
|
|
|
109
128
|
centred under the arrows and counts the prompts in the session; it disappears
|
|
110
129
|
once the viewport is following output again.
|
|
111
130
|
|
|
131
|
+
Pi 0.85.0 moves the prompt cursor to wherever you click in the prompt. If you
|
|
132
|
+
click the prompt area to focus the terminal or to select text, turn off
|
|
133
|
+
`Prompt mouse clicks` in `/settings`: clicks on the prompt text then leave the
|
|
134
|
+
cursor alone while drag-to-select keeps working. The same menu holds the
|
|
135
|
+
`Session action rail` toggle. Both persist in `~/.pi/agent/proper-base.json`
|
|
136
|
+
as `editorMouse` and `sessionRail`.
|
|
137
|
+
|
|
112
138
|
### Clipboard and model image context
|
|
113
139
|
|
|
114
140
|
Ctrl+V and Ctrl+Shift+V both use Pi's image-or-text clipboard action. Readable
|
|
@@ -158,7 +184,7 @@ highlight. Custom replacement footers are not changed.
|
|
|
158
184
|
|
|
159
185
|
## Install
|
|
160
186
|
|
|
161
|
-
Node 22.19 or newer is required. The package is tested against Pi 0.
|
|
187
|
+
Node 22.19 or newer is required. The package is tested against Pi 0.85.0.
|
|
162
188
|
|
|
163
189
|
From npm:
|
|
164
190
|
|
package/index.ts
CHANGED
|
@@ -23,10 +23,12 @@ import {
|
|
|
23
23
|
installAutocompleteDetails,
|
|
24
24
|
installInlineSlashAutocomplete,
|
|
25
25
|
installModelAutocompleteSubmit,
|
|
26
|
+
modelThinkingCommand,
|
|
26
27
|
sortModelAutocompleteDescending,
|
|
27
28
|
} from "./src/autocomplete-details.ts";
|
|
28
29
|
import { installClipboardLeakGuard } from "./src/clipboard-guard.ts";
|
|
29
30
|
import { commitGuardReason } from "./src/commit-guard.ts";
|
|
31
|
+
import { installEditorMouseGuard } from "./src/editor-mouse.ts";
|
|
30
32
|
import {
|
|
31
33
|
installEditorNavigation,
|
|
32
34
|
installPromptClear,
|
|
@@ -62,8 +64,13 @@ import { installPromptJump } from "./src/prompt-jump.ts";
|
|
|
62
64
|
import { installRecorder } from "./src/recorder.ts";
|
|
63
65
|
import { installSelectionDismiss } from "./src/selection-dismiss.ts";
|
|
64
66
|
import { installFastSessionList } from "./src/session-list.ts";
|
|
67
|
+
import { installSettings, type SettingsController } from "./src/settings.ts";
|
|
65
68
|
import { pinSkillContext } from "./src/skill-context.ts";
|
|
66
69
|
import { installSmartSelection } from "./src/smart-selection.ts";
|
|
70
|
+
import {
|
|
71
|
+
persistDefaultModel,
|
|
72
|
+
persistDefaultThinkingLevel,
|
|
73
|
+
} from "./src/startup-defaults.ts";
|
|
67
74
|
import {
|
|
68
75
|
appendPrompt,
|
|
69
76
|
compactIfNeeded,
|
|
@@ -290,6 +297,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
290
297
|
let removeFooterColors: (() => void) | undefined;
|
|
291
298
|
let removeJumpToBottom: (() => void) | undefined;
|
|
292
299
|
let removePromptJump: (() => void) | undefined;
|
|
300
|
+
let settings: SettingsController | undefined;
|
|
301
|
+
let removeEditorMouseGuard: (() => void) | undefined;
|
|
293
302
|
let removePromptClear: (() => void) | undefined;
|
|
294
303
|
let removeSmartSelection: (() => void) | undefined;
|
|
295
304
|
let removeSelectionDismiss: (() => void) | undefined;
|
|
@@ -326,6 +335,43 @@ export default function (pi: ExtensionAPI) {
|
|
|
326
335
|
);
|
|
327
336
|
};
|
|
328
337
|
|
|
338
|
+
/**
|
|
339
|
+
* Run a `/model <reference> <level>` submission, if that is the shape.
|
|
340
|
+
*
|
|
341
|
+
* Pi reads a `/model` argument as one model search term, so a thinking level
|
|
342
|
+
* appended to it only widens that search and opens the picker. The command is
|
|
343
|
+
* taken over at the editor instead, and the level is applied after the model:
|
|
344
|
+
* every `setModel` recomputes the level from settings, so setting it first
|
|
345
|
+
* would be discarded. A reference matching no model is left to Pi, whose
|
|
346
|
+
* picker still opens on the raw text.
|
|
347
|
+
*/
|
|
348
|
+
const applyModelThinking = (ctx: ExtensionContext, text: string): boolean => {
|
|
349
|
+
const request = modelThinkingCommand(text);
|
|
350
|
+
if (!request) return false;
|
|
351
|
+
const available =
|
|
352
|
+
ctx.scopedModels.length > 0
|
|
353
|
+
? ctx.scopedModels.map((scoped) => scoped.model)
|
|
354
|
+
: ctx.modelRegistry.getAvailable();
|
|
355
|
+
const model = available.find(
|
|
356
|
+
(candidate) =>
|
|
357
|
+
`${candidate.provider}/${candidate.id}` === request.reference ||
|
|
358
|
+
candidate.id === request.reference,
|
|
359
|
+
);
|
|
360
|
+
if (!model) return false;
|
|
361
|
+
void (async () => {
|
|
362
|
+
if (!(await pi.setModel(model))) {
|
|
363
|
+
ctx.ui.notify(
|
|
364
|
+
`Could not switch to ${model.provider}/${model.id}`,
|
|
365
|
+
"error",
|
|
366
|
+
);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
pi.setThinkingLevel(request.level);
|
|
370
|
+
ctx.ui.notify(`Model: ${model.id} (thinking: ${request.level})`, "info");
|
|
371
|
+
})();
|
|
372
|
+
return true;
|
|
373
|
+
};
|
|
374
|
+
|
|
329
375
|
pi.registerCommand?.(RESTORE_MODEL_COMMAND, {
|
|
330
376
|
description: "Internal: restore the model after /clear",
|
|
331
377
|
handler: async (args, ctx) => {
|
|
@@ -389,6 +435,20 @@ export default function (pi: ExtensionAPI) {
|
|
|
389
435
|
},
|
|
390
436
|
});
|
|
391
437
|
|
|
438
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Sticky startup defaults]]
|
|
439
|
+
pi.on("model_select", (event) => {
|
|
440
|
+
if (event.source === "restore") return;
|
|
441
|
+
persistDefaultModel(getAgentDir(), event.model);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
pi.on("thinking_level_select", (event, ctx) => {
|
|
445
|
+
persistDefaultThinkingLevel(
|
|
446
|
+
getAgentDir(),
|
|
447
|
+
event.level,
|
|
448
|
+
ctx.model ?? undefined,
|
|
449
|
+
);
|
|
450
|
+
});
|
|
451
|
+
|
|
392
452
|
pi.on("input", (event) => {
|
|
393
453
|
if (event.source === "interactive") {
|
|
394
454
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Pinned transcript scrolling]]
|
|
@@ -568,6 +628,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
568
628
|
removeJumpToBottom = undefined;
|
|
569
629
|
removePromptJump?.();
|
|
570
630
|
removePromptJump = undefined;
|
|
631
|
+
settings?.dispose();
|
|
632
|
+
settings = undefined;
|
|
633
|
+
removeEditorMouseGuard?.();
|
|
634
|
+
removeEditorMouseGuard = undefined;
|
|
571
635
|
removePromptClear?.();
|
|
572
636
|
removePromptClear = undefined;
|
|
573
637
|
removeSmartSelection?.();
|
|
@@ -635,7 +699,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
635
699
|
ctx.ui.setEditorText(promptText);
|
|
636
700
|
return undefined;
|
|
637
701
|
});
|
|
638
|
-
ctx.ui.addAutocompleteProvider?.(
|
|
702
|
+
ctx.ui.addAutocompleteProvider?.((current) =>
|
|
703
|
+
sortModelAutocompleteDescending(current, () => pi.getThinkingLevel?.()),
|
|
704
|
+
);
|
|
639
705
|
|
|
640
706
|
const store = storePath(getAgentDir(), ctx.cwd);
|
|
641
707
|
compactIfNeeded(store);
|
|
@@ -697,9 +763,22 @@ export default function (pi: ExtensionAPI) {
|
|
|
697
763
|
removeJumpToBottom?.();
|
|
698
764
|
removeJumpToBottom = installJumpToBottom(editor, tui);
|
|
699
765
|
removePromptJump?.();
|
|
766
|
+
settings?.dispose();
|
|
767
|
+
settings = installSettings(tui, editor, getAgentDir());
|
|
768
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Prompt mouse clicks]]
|
|
769
|
+
removeEditorMouseGuard?.();
|
|
770
|
+
removeEditorMouseGuard = installEditorMouseGuard(
|
|
771
|
+
editor,
|
|
772
|
+
() => settings?.editorMouse() !== false,
|
|
773
|
+
);
|
|
774
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Session action rail]]
|
|
700
775
|
removePromptJump = installPromptJump(tui, {
|
|
701
776
|
color: (value) => ctx.ui.theme.fg("muted", value),
|
|
702
777
|
subtle: (value) => ctx.ui.theme.fg("dim", value),
|
|
778
|
+
outline: () =>
|
|
779
|
+
settings?.enabled() !== false
|
|
780
|
+
? (transcriptCleanup?.outline() ?? [])
|
|
781
|
+
: [],
|
|
703
782
|
});
|
|
704
783
|
imagePreview?.dispose();
|
|
705
784
|
imagePreview = installImagePreview(editor, tui, {
|
|
@@ -716,15 +795,18 @@ export default function (pi: ExtensionAPI) {
|
|
|
716
795
|
// provider's global `/fast` can only be repurposed as a session
|
|
717
796
|
// toggle by consuming it at the editor before pi ever parses it.
|
|
718
797
|
(text) => {
|
|
719
|
-
if (
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
798
|
+
if (isFastToggle(text)) {
|
|
799
|
+
const notice = fastToggleNotice({
|
|
800
|
+
scope: "session",
|
|
801
|
+
enabled: fastOverlay.toggleSession(),
|
|
802
|
+
otherEnabled: fastOverlay.isGlobalEnabled(),
|
|
803
|
+
modelSupported: fastOverlay.supportsModel(ctx.model ?? undefined),
|
|
804
|
+
});
|
|
805
|
+
ctx.ui.notify(notice.message, notice.level);
|
|
806
|
+
return true;
|
|
807
|
+
}
|
|
808
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Model thinking argument]]
|
|
809
|
+
return applyModelThinking(ctx, text);
|
|
728
810
|
},
|
|
729
811
|
);
|
|
730
812
|
installModelAutocompleteSubmit(editor, keybindings);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "proper-base",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Baseline Pi behavior for session naming, history, prompt editing, fullscreen navigation, and footer styling",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -53,8 +53,9 @@
|
|
|
53
53
|
"@earendil-works/pi-tui": "*"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@earendil-works/pi-coding-agent": "0.
|
|
57
|
-
"@earendil-works/pi-
|
|
56
|
+
"@earendil-works/pi-coding-agent": "0.85.0",
|
|
57
|
+
"@earendil-works/pi-server": "0.85.0",
|
|
58
|
+
"@earendil-works/pi-tui": "0.85.0",
|
|
58
59
|
"@types/node": "25.9.5",
|
|
59
60
|
"typescript": "6.0.2"
|
|
60
61
|
},
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type AutocompleteItem,
|
|
2
3
|
type AutocompleteProvider,
|
|
4
|
+
type AutocompleteSuggestions,
|
|
3
5
|
type Component,
|
|
4
6
|
type OverlayHandle,
|
|
5
7
|
type OverlayMargin,
|
|
@@ -29,6 +31,67 @@ const MODEL_ORDER = new Intl.Collator("en", {
|
|
|
29
31
|
sensitivity: "base",
|
|
30
32
|
});
|
|
31
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Pi's thinking levels, weakest first. The extension API exports neither the
|
|
36
|
+
* list nor the per-model subset, and Pi clamps an unsupported level when it
|
|
37
|
+
* applies one, so restating the full list here costs nothing.
|
|
38
|
+
*/
|
|
39
|
+
const THINKING_LEVELS = [
|
|
40
|
+
"off",
|
|
41
|
+
"minimal",
|
|
42
|
+
"low",
|
|
43
|
+
"medium",
|
|
44
|
+
"high",
|
|
45
|
+
"xhigh",
|
|
46
|
+
"max",
|
|
47
|
+
] as const;
|
|
48
|
+
|
|
49
|
+
export type ThinkingArgument = (typeof THINKING_LEVELS)[number];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* `/model <provider>/<id> <level>` before the cursor.
|
|
53
|
+
*
|
|
54
|
+
* The first argument must carry a provider slash, the exact shape a model
|
|
55
|
+
* completion produces, so a multi-term model search such as `/model opus 4`
|
|
56
|
+
* keeps filtering models instead of being read as a level.
|
|
57
|
+
*/
|
|
58
|
+
const MODEL_THINKING_ARGUMENT = /^\/model\s+(\S*\/\S*)\s+(\S*)$/;
|
|
59
|
+
|
|
60
|
+
/** Level suggestions for a second `/model` argument, or nothing for any other shape. */
|
|
61
|
+
function thinkingSuggestions(
|
|
62
|
+
activeLine: string,
|
|
63
|
+
current: string | undefined,
|
|
64
|
+
): AutocompleteSuggestions | undefined {
|
|
65
|
+
const prefix = MODEL_THINKING_ARGUMENT.exec(activeLine)?.[2];
|
|
66
|
+
if (prefix === undefined) return undefined;
|
|
67
|
+
const query = prefix.toLowerCase();
|
|
68
|
+
const matches = THINKING_LEVELS.filter((level) => level.startsWith(query));
|
|
69
|
+
// The level already in effect leads the list, so accepting the menu's
|
|
70
|
+
// default selection keeps the current level instead of the weakest one.
|
|
71
|
+
const items: AutocompleteItem[] = [
|
|
72
|
+
...matches.filter((level) => level === current),
|
|
73
|
+
...matches.filter((level) => level !== current),
|
|
74
|
+
].map((level) => ({ value: level, label: level }));
|
|
75
|
+
return items.length > 0 ? { items, prefix } : undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A submitted `/model <reference> <level>`, if that is the shape.
|
|
80
|
+
*
|
|
81
|
+
* Pi's own `/model` treats everything after the command as one model search
|
|
82
|
+
* term, so the extra argument has to be split off before Pi ever sees it.
|
|
83
|
+
*/
|
|
84
|
+
export function modelThinkingCommand(
|
|
85
|
+
text: string,
|
|
86
|
+
): { reference: string; level: ThinkingArgument } | undefined {
|
|
87
|
+
const match = /^\/model\s+(\S+)\s+(\S+)$/.exec(text.trim());
|
|
88
|
+
const reference = match?.[1];
|
|
89
|
+
const level = THINKING_LEVELS.find(
|
|
90
|
+
(candidate) => candidate === match?.[2]?.toLowerCase(),
|
|
91
|
+
);
|
|
92
|
+
return reference && level ? { reference, level } : undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
32
95
|
type AutocompleteEditor = Component & {
|
|
33
96
|
autocompleteList?: {
|
|
34
97
|
getSelectedItem(): { description?: string } | null;
|
|
@@ -45,6 +108,7 @@ type InlineSlashEditor = Component & {
|
|
|
45
108
|
handleInput?(data: string): void;
|
|
46
109
|
isShowingAutocomplete?(): boolean;
|
|
47
110
|
tryTriggerAutocomplete?(): void;
|
|
111
|
+
updateAutocomplete?(): void;
|
|
48
112
|
[INLINE_SLASH_INSTALLED]?: boolean;
|
|
49
113
|
};
|
|
50
114
|
|
|
@@ -54,7 +118,7 @@ type ModelAutocompleteEditor = Component & {
|
|
|
54
118
|
};
|
|
55
119
|
getText?(): string;
|
|
56
120
|
handleInput?(data: string): void;
|
|
57
|
-
|
|
121
|
+
tryTriggerAutocomplete?(): void;
|
|
58
122
|
[MODEL_SUBMIT_INSTALLED]?: boolean;
|
|
59
123
|
};
|
|
60
124
|
|
|
@@ -104,12 +168,49 @@ export function installInlineSlashAutocomplete(editor: Component): void {
|
|
|
104
168
|
|
|
105
169
|
const handleInput = target.handleInput.bind(target);
|
|
106
170
|
target.handleInput = (data: string) => {
|
|
171
|
+
const wasShowing = target.isShowingAutocomplete?.() === true;
|
|
172
|
+
const before = wasShowing ? target.state?.lines.join("\n") : undefined;
|
|
107
173
|
handleInput(data);
|
|
108
|
-
if (target.isShowingAutocomplete?.() || !/^[/a-zA-Z0-9._:-]$/.test(data)) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
174
|
const state = target.state;
|
|
112
175
|
if (!state) return;
|
|
176
|
+
if (target.isShowingAutocomplete?.()) {
|
|
177
|
+
// Word and line deletes (alt+backspace, ctrl+w/u/k) edit the prompt
|
|
178
|
+
// without the autocomplete refresh pi's insert and backspace paths
|
|
179
|
+
// run, so an open list lingers under stale text. Re-request with
|
|
180
|
+
// the current prompt; an emptied context closes the list through
|
|
181
|
+
// the editor's own no-suggestions path. Single printable keys and
|
|
182
|
+
// backspace already refreshed inside the editor.
|
|
183
|
+
if (
|
|
184
|
+
wasShowing &&
|
|
185
|
+
state.lines.join("\n") !== before &&
|
|
186
|
+
!(data.length === 1 && data >= " ")
|
|
187
|
+
) {
|
|
188
|
+
target.updateAutocomplete?.();
|
|
189
|
+
}
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (wasShowing) {
|
|
193
|
+
// Accepting a command completion closes the list and leaves
|
|
194
|
+
// "/command " behind, but pi re-opens suggestions only from typed
|
|
195
|
+
// characters, so an accepted command's argument menu — /model's
|
|
196
|
+
// list — stayed hidden until another keystroke. A text change
|
|
197
|
+
// during the open-to-closed transition is what separates
|
|
198
|
+
// acceptance from dismissal: Esc leaves the prompt untouched and
|
|
199
|
+
// must not reopen the list just closed. The token shape keeps a
|
|
200
|
+
// completed file path from popping a menu of its own.
|
|
201
|
+
const beforeCursor = (state.lines[state.cursorLine] ?? "").slice(
|
|
202
|
+
0,
|
|
203
|
+
state.cursorCol,
|
|
204
|
+
);
|
|
205
|
+
if (
|
|
206
|
+
state.lines.join("\n") !== before &&
|
|
207
|
+
/(?:^|\s)\/[a-zA-Z0-9._:-]+ $/.test(beforeCursor)
|
|
208
|
+
) {
|
|
209
|
+
target.tryTriggerAutocomplete?.();
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (!/^[/a-zA-Z0-9._:-]$/.test(data)) return;
|
|
113
214
|
if (inlineSlashContext(state.lines, state.cursorLine, state.cursorCol)) {
|
|
114
215
|
target.tryTriggerAutocomplete?.();
|
|
115
216
|
}
|
|
@@ -163,6 +264,7 @@ function createAutocompleteDetailBox(theme: DetailTheme) {
|
|
|
163
264
|
|
|
164
265
|
export function sortModelAutocompleteDescending(
|
|
165
266
|
current: AutocompleteProvider,
|
|
267
|
+
thinkingLevel?: () => string | undefined,
|
|
166
268
|
): AutocompleteProvider {
|
|
167
269
|
return {
|
|
168
270
|
...(current.triggerCharacters
|
|
@@ -170,6 +272,13 @@ export function sortModelAutocompleteDescending(
|
|
|
170
272
|
: {}),
|
|
171
273
|
async getSuggestions(lines, cursorLine, cursorCol, options) {
|
|
172
274
|
const inline = inlineSlashContext(lines, cursorLine, cursorCol);
|
|
275
|
+
const activeLine =
|
|
276
|
+
inline?.prefix ?? (lines[cursorLine] ?? "").slice(0, cursorCol);
|
|
277
|
+
// Checked before delegating so an explicit Tab, which asks Pi for
|
|
278
|
+
// forced file completion once the argument holds a space, still lists
|
|
279
|
+
// levels rather than paths.
|
|
280
|
+
const thinking = thinkingSuggestions(activeLine, thinkingLevel?.());
|
|
281
|
+
if (thinking) return thinking;
|
|
173
282
|
const requestLines = inline ? [...lines] : lines;
|
|
174
283
|
if (inline) requestLines[cursorLine] = inline.prefix;
|
|
175
284
|
const suggestions = await current.getSuggestions(
|
|
@@ -178,8 +287,6 @@ export function sortModelAutocompleteDescending(
|
|
|
178
287
|
inline ? inline.prefix.length : cursorCol,
|
|
179
288
|
inline ? { ...options, force: false } : options,
|
|
180
289
|
);
|
|
181
|
-
const line = lines[cursorLine] ?? "";
|
|
182
|
-
const activeLine = inline?.prefix ?? line.slice(0, cursorCol);
|
|
183
290
|
if (!suggestions || !activeLine.startsWith("/model ")) {
|
|
184
291
|
return suggestions;
|
|
185
292
|
}
|
|
@@ -247,6 +354,13 @@ export function sortModelAutocompleteDescending(
|
|
|
247
354
|
};
|
|
248
355
|
}
|
|
249
356
|
|
|
357
|
+
/** Whether an accepted completion left a runnable `/model` command behind. */
|
|
358
|
+
function completedModelCommand(text: string, value: string): boolean {
|
|
359
|
+
if (!/^\/model [^\n]*$/.test(text)) return false;
|
|
360
|
+
const argument = text.slice("/model ".length);
|
|
361
|
+
return argument === value || argument.endsWith(` ${value}`);
|
|
362
|
+
}
|
|
363
|
+
|
|
250
364
|
export function installModelAutocompleteSubmit(
|
|
251
365
|
editor: Component,
|
|
252
366
|
keybindings: EditorKeybindings,
|
|
@@ -261,28 +375,47 @@ export function installModelAutocompleteSubmit(
|
|
|
261
375
|
}
|
|
262
376
|
|
|
263
377
|
const handleInput = target.handleInput.bind(target);
|
|
378
|
+
const openThinkingMenu = () => {
|
|
379
|
+
if (
|
|
380
|
+
target.autocompleteList === undefined &&
|
|
381
|
+
MODEL_THINKING_ARGUMENT.test(target.getText?.() ?? "")
|
|
382
|
+
) {
|
|
383
|
+
target.tryTriggerAutocomplete?.();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
264
386
|
target.handleInput = (data: string) => {
|
|
265
|
-
const
|
|
266
|
-
const
|
|
387
|
+
const selectedValue = target.autocompleteList?.getSelectedItem()?.value;
|
|
388
|
+
const accepted =
|
|
389
|
+
typeof selectedValue === "string" &&
|
|
390
|
+
/^\/model [^\n]*$/.test(target.getText?.() ?? "")
|
|
391
|
+
? selectedValue
|
|
392
|
+
: undefined;
|
|
267
393
|
const confirm = keybindings.matches(data, "tui.select.confirm");
|
|
268
394
|
const tab = keybindings.matches(data, "tui.input.tab");
|
|
269
|
-
const shouldSubmit =
|
|
270
|
-
(confirm || tab) &&
|
|
271
|
-
typeof selectedValue === "string" &&
|
|
272
|
-
/^\/model [^\n]*$/.test(target.getText?.() ?? "");
|
|
273
395
|
|
|
274
396
|
handleInput(data);
|
|
397
|
+
// Pi reopens suggestions only from typed characters, and the separator
|
|
398
|
+
// before a thinking level is a space, which is not one of them.
|
|
399
|
+
if (data === " ") {
|
|
400
|
+
openThinkingMenu();
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
275
403
|
if (
|
|
276
|
-
|
|
277
|
-
target.autocompleteList
|
|
278
|
-
target.getText?.()
|
|
404
|
+
accepted === undefined ||
|
|
405
|
+
target.autocompleteList !== undefined ||
|
|
406
|
+
!completedModelCommand(target.getText?.() ?? "", accepted)
|
|
279
407
|
) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
// Tab stops at the model name so a level can follow, and Pi's argument
|
|
411
|
+
// completion appends no separator, so the space and the menu it cannot
|
|
412
|
+
// trigger on its own are supplied here. Tab on the level itself has
|
|
413
|
+
// nothing left to complete, so it submits like Enter.
|
|
414
|
+
if (tab && accepted.includes("/")) {
|
|
415
|
+
handleInput(" ");
|
|
416
|
+
openThinkingMenu();
|
|
417
|
+
} else if (confirm || tab) {
|
|
418
|
+
handleInput("\r");
|
|
286
419
|
}
|
|
287
420
|
};
|
|
288
421
|
target[MODEL_SUBMIT_INSTALLED] = true;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Component,
|
|
3
|
+
TuiMouseEvent,
|
|
4
|
+
TuiMouseEventResult,
|
|
5
|
+
} from "@earendil-works/pi-tui";
|
|
6
|
+
|
|
7
|
+
const INSTALLED = Symbol.for("pi-proper-base.editor-mouse");
|
|
8
|
+
|
|
9
|
+
type MouseHandler = (event: TuiMouseEvent) => TuiMouseEventResult | undefined;
|
|
10
|
+
type MouseEditor = Component & {
|
|
11
|
+
handleMouse?: MouseHandler;
|
|
12
|
+
renderedVisibleLineCount?: number;
|
|
13
|
+
[INSTALLED]?: () => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Keep prompt clicks from moving the editor cursor while the setting is off.
|
|
18
|
+
*
|
|
19
|
+
* Since Pi 0.85.0 the fullscreen renderer routes mouse input through
|
|
20
|
+
* component `handleMouse` methods, and the editor's own handler turns a left
|
|
21
|
+
* click on a prompt row into a cursor move. That is the wrong default for
|
|
22
|
+
* anyone who clicks the prompt area to focus the terminal or to select text:
|
|
23
|
+
* the cursor jumps away from where typing left it. This wraps the editor
|
|
24
|
+
* instance's `handleMouse` and, while `enabled()` is false, swallows left
|
|
25
|
+
* clicks that land on the prompt's text rows. The event is still reported
|
|
26
|
+
* handled so the click neither falls through to the renderer's selection
|
|
27
|
+
* path nor moves focus, and the drag-to-select gesture is untouched because
|
|
28
|
+
* the editor already leaves press, drag, and release to the renderer.
|
|
29
|
+
*
|
|
30
|
+
* Clicks on the autocomplete rows below the prompt, and on the border rows,
|
|
31
|
+
* keep Pi's native behavior: the guard only covers the text rows Pi itself
|
|
32
|
+
* bounds with `renderedVisibleLineCount`. Editors without a `handleMouse`
|
|
33
|
+
* (Pi before 0.85.0, or a renderer without mouse dispatch) install nothing.
|
|
34
|
+
*
|
|
35
|
+
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Prompt mouse clicks]]
|
|
36
|
+
*/
|
|
37
|
+
export function installEditorMouseGuard(
|
|
38
|
+
editor: Component,
|
|
39
|
+
enabled: () => boolean,
|
|
40
|
+
): (() => void) | undefined {
|
|
41
|
+
const target = editor as MouseEditor;
|
|
42
|
+
target[INSTALLED]?.();
|
|
43
|
+
if (typeof target.handleMouse !== "function") return undefined;
|
|
44
|
+
|
|
45
|
+
const hadOwnMethod = Object.hasOwn(target, "handleMouse");
|
|
46
|
+
const original = target.handleMouse;
|
|
47
|
+
const wrapped: MouseHandler = function (this: MouseEditor, event) {
|
|
48
|
+
if (
|
|
49
|
+
!enabled() &&
|
|
50
|
+
event.type === "click" &&
|
|
51
|
+
event.button === "left" &&
|
|
52
|
+
event.y > 0 &&
|
|
53
|
+
event.y <= (target.renderedVisibleLineCount ?? 0)
|
|
54
|
+
) {
|
|
55
|
+
return { handled: true };
|
|
56
|
+
}
|
|
57
|
+
return original.call(this, event);
|
|
58
|
+
};
|
|
59
|
+
target.handleMouse = wrapped;
|
|
60
|
+
|
|
61
|
+
const dispose = () => {
|
|
62
|
+
if (target.handleMouse !== wrapped) return;
|
|
63
|
+
if (hadOwnMethod) target.handleMouse = original;
|
|
64
|
+
else delete target.handleMouse;
|
|
65
|
+
delete target[INSTALLED];
|
|
66
|
+
};
|
|
67
|
+
target[INSTALLED] = dispose;
|
|
68
|
+
return dispose;
|
|
69
|
+
}
|
package/src/editor-navigation.ts
CHANGED
|
@@ -50,6 +50,7 @@ type NavigableEditor = Component & {
|
|
|
50
50
|
isShowingAutocomplete?(): boolean;
|
|
51
51
|
onChange?(text: string): void;
|
|
52
52
|
setText?(text: string): void;
|
|
53
|
+
historyIndex?: number;
|
|
53
54
|
state?: {
|
|
54
55
|
lines: string[];
|
|
55
56
|
cursorLine: number;
|
|
@@ -80,6 +81,35 @@ export type ReverseHistorySearchController = {
|
|
|
80
81
|
reset(prompts: readonly string[]): void;
|
|
81
82
|
};
|
|
82
83
|
|
|
84
|
+
/** The large-paste registry pi-tui's `setText()` clears alongside the text. */
|
|
85
|
+
type PasteRegistryEditor = {
|
|
86
|
+
setText?(text: string): void;
|
|
87
|
+
pastes?: Map<number, string>;
|
|
88
|
+
pasteCounter?: number;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// pi-tui's `setText()` empties its `[paste #N]` registry even when the new
|
|
92
|
+
// text keeps the markers; submission would then send the literal tag instead
|
|
93
|
+
// of the pasted content. Restore the entries the new text still references,
|
|
94
|
+
// and the id counter with them so the next paste cannot collide with a kept
|
|
95
|
+
// id.
|
|
96
|
+
export function setTextKeepingPastes(editor: unknown, text: string): void {
|
|
97
|
+
const target = editor as PasteRegistryEditor;
|
|
98
|
+
const registry =
|
|
99
|
+
target.pastes instanceof Map ? [...target.pastes] : undefined;
|
|
100
|
+
const counter = target.pasteCounter;
|
|
101
|
+
target.setText?.(text);
|
|
102
|
+
if (!registry?.length || !(target.pastes instanceof Map)) return;
|
|
103
|
+
let kept = false;
|
|
104
|
+
for (const [id, content] of registry) {
|
|
105
|
+
const marker = `[paste #${id}`;
|
|
106
|
+
if (!text.includes(`${marker}]`) && !text.includes(`${marker} `)) continue;
|
|
107
|
+
target.pastes.set(id, content);
|
|
108
|
+
kept = true;
|
|
109
|
+
}
|
|
110
|
+
if (kept && typeof counter === "number") target.pasteCounter = counter;
|
|
111
|
+
}
|
|
112
|
+
|
|
83
113
|
function segmentImageMarkers(
|
|
84
114
|
text: string,
|
|
85
115
|
segments: Iterable<TextSegment>,
|
|
@@ -197,6 +227,25 @@ export function installEditorNavigation(
|
|
|
197
227
|
}
|
|
198
228
|
}
|
|
199
229
|
if (previous) {
|
|
230
|
+
// Pi recalls history on Up from the first visual row whenever the
|
|
231
|
+
// cursor sits at column 0, so Home followed by Up, or Up pressed
|
|
232
|
+
// twice from a short first line, replaces a draft with an older
|
|
233
|
+
// prompt. Recall is only for an empty prompt: with a draft and no
|
|
234
|
+
// recall in progress, Up at that position is already at the line
|
|
235
|
+
// start Pi would otherwise jump to, so it does nothing. The dedicated
|
|
236
|
+
// history binding is left alone; it is opt-in and explicit.
|
|
237
|
+
if (
|
|
238
|
+
keybindings.matches(data, "tui.editor.cursorUp") &&
|
|
239
|
+
(target.historyIndex ?? -1) < 0 &&
|
|
240
|
+
target.getCursor?.().col === 0 &&
|
|
241
|
+
target.getLines?.().some((line) => line !== "")
|
|
242
|
+
) {
|
|
243
|
+
const visualLines = target.buildVisualLineMap?.(target.lastWidth ?? 80);
|
|
244
|
+
const visualIndex = visualLines
|
|
245
|
+
? target.findCurrentVisualLine?.(visualLines)
|
|
246
|
+
: undefined;
|
|
247
|
+
if (visualIndex === 0) return;
|
|
248
|
+
}
|
|
200
249
|
const before = target.getLines?.().join("\n");
|
|
201
250
|
handleInput(data);
|
|
202
251
|
const state = target.state;
|
|
@@ -459,7 +508,9 @@ export function installReverseHistorySearch(
|
|
|
459
508
|
if (target.isShowingAutocomplete?.()) handleInput("\x1b");
|
|
460
509
|
const cursor = target.getCursor?.();
|
|
461
510
|
// Exit Pi's native Up/Down history mode without adding an undo entry.
|
|
462
|
-
|
|
511
|
+
// Its setText also wipes the large-paste registry while the draft keeps
|
|
512
|
+
// its markers, so the entries the draft references are restored.
|
|
513
|
+
setTextKeepingPastes(target, draft);
|
|
463
514
|
search = {
|
|
464
515
|
draft,
|
|
465
516
|
cursor,
|