proper-base 0.7.0 → 0.8.0
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 +240 -39
- package/index.ts +189 -25
- package/package.json +1 -1
- package/src/autocomplete-details.ts +1 -1
- package/src/context-tokens.ts +340 -0
- package/src/fast-mode.ts +1 -1
- package/src/footer-colors.ts +4 -3
- package/src/proactive-delegation.ts +15 -4
- package/src/prompt-display-host.ts +30 -0
- package/src/settings.ts +94 -30
- package/src/skill-context.ts +229 -188
- package/src/skills.ts +587 -0
- package/src/sticky-defaults.ts +4 -1
- package/src/transcript-cleanup.ts +8 -2
package/index.ts
CHANGED
|
@@ -26,11 +26,23 @@ import {
|
|
|
26
26
|
installModelAutocompleteSubmit,
|
|
27
27
|
modelThinkingCommand,
|
|
28
28
|
sortModelAutocompleteDescending,
|
|
29
|
+
THINKING_LEVELS,
|
|
29
30
|
} from "./src/autocomplete-details.ts";
|
|
30
31
|
import { installBaseKeybindings } from "./src/base-keybindings.ts";
|
|
31
32
|
import { installClipboardLeakGuard } from "./src/clipboard-guard.ts";
|
|
32
33
|
import { installClipboardSelection } from "./src/clipboard-selection.ts";
|
|
33
34
|
import { commitGuardReason } from "./src/commit-guard.ts";
|
|
35
|
+
import {
|
|
36
|
+
asTokensSession,
|
|
37
|
+
CONTEXT_TOKENS_ENTRY,
|
|
38
|
+
ContextTokens,
|
|
39
|
+
type ContextTokensMode,
|
|
40
|
+
installTokensScopeCompletion,
|
|
41
|
+
parseTokensArgs,
|
|
42
|
+
TOKENS_USAGE,
|
|
43
|
+
tokensArgumentCompletions,
|
|
44
|
+
tokensNotice,
|
|
45
|
+
} from "./src/context-tokens.ts";
|
|
34
46
|
import { installEditorMouseGuard } from "./src/editor-mouse.ts";
|
|
35
47
|
import {
|
|
36
48
|
installEditorNavigation,
|
|
@@ -75,7 +87,7 @@ import { installRecorder } from "./src/recorder.ts";
|
|
|
75
87
|
import { installSelectionDismiss } from "./src/selection-dismiss.ts";
|
|
76
88
|
import { installFastSessionList } from "./src/session-list.ts";
|
|
77
89
|
import { installSettings, type SettingsController } from "./src/settings.ts";
|
|
78
|
-
import {
|
|
90
|
+
import { registerSkillContext } from "./src/skills.ts";
|
|
79
91
|
import { installSmartSelection } from "./src/smart-selection.ts";
|
|
80
92
|
import { stickyDefaultsEnabled } from "./src/startup-defaults.ts";
|
|
81
93
|
import { installStickyDefaultsAdapter } from "./src/sticky-defaults.ts";
|
|
@@ -169,30 +181,44 @@ function extractSessionTitle(text: string): string | undefined {
|
|
|
169
181
|
);
|
|
170
182
|
}
|
|
171
183
|
|
|
172
|
-
type
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
184
|
+
type ClearSettings = {
|
|
185
|
+
provider: string;
|
|
186
|
+
id: string;
|
|
187
|
+
thinkingLevel: ReturnType<ExtensionAPI["getThinkingLevel"]>;
|
|
188
|
+
sessionFast: boolean;
|
|
189
|
+
contextTokens?: ContextTokensMode | undefined;
|
|
190
|
+
};
|
|
179
191
|
|
|
180
|
-
function
|
|
192
|
+
function decodeClearSettings(value: string): ClearSettings | undefined {
|
|
181
193
|
try {
|
|
182
194
|
const parsed = JSON.parse(
|
|
183
195
|
decodeURIComponent(value.trim()),
|
|
184
|
-
) as Partial<
|
|
196
|
+
) as Partial<ClearSettings> | null;
|
|
185
197
|
if (
|
|
186
198
|
!parsed ||
|
|
187
199
|
typeof parsed.provider !== "string" ||
|
|
188
200
|
!parsed.provider ||
|
|
189
201
|
typeof parsed.id !== "string" ||
|
|
190
|
-
!parsed.id
|
|
202
|
+
!parsed.id ||
|
|
203
|
+
!parsed.thinkingLevel ||
|
|
204
|
+
!THINKING_LEVELS.includes(parsed.thinkingLevel) ||
|
|
205
|
+
typeof parsed.sessionFast !== "boolean" ||
|
|
206
|
+
(parsed.contextTokens !== undefined &&
|
|
207
|
+
parsed.contextTokens !== "max" &&
|
|
208
|
+
parsed.contextTokens !== "default")
|
|
191
209
|
)
|
|
192
210
|
return undefined;
|
|
193
|
-
return {
|
|
194
|
-
|
|
195
|
-
|
|
211
|
+
return {
|
|
212
|
+
provider: parsed.provider,
|
|
213
|
+
id: parsed.id,
|
|
214
|
+
thinkingLevel: parsed.thinkingLevel,
|
|
215
|
+
sessionFast: parsed.sessionFast,
|
|
216
|
+
contextTokens: parsed.contextTokens,
|
|
217
|
+
};
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (error instanceof URIError || error instanceof SyntaxError)
|
|
220
|
+
return undefined;
|
|
221
|
+
throw error;
|
|
196
222
|
}
|
|
197
223
|
}
|
|
198
224
|
|
|
@@ -208,6 +234,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
208
234
|
const stickyDefaults = installStickyDefaultsAdapter(() =>
|
|
209
235
|
stickyDefaultsEnabled(getAgentDir()),
|
|
210
236
|
);
|
|
237
|
+
const skills = registerSkillContext(pi, getAgentDir(), (ctx) =>
|
|
238
|
+
stickyDefaults.session(ctx.sessionManager),
|
|
239
|
+
);
|
|
211
240
|
|
|
212
241
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Fast tier scopes]]
|
|
213
242
|
const fastOverlay = new FastOverlay(getAgentDir());
|
|
@@ -247,6 +276,99 @@ export default function (pi: ExtensionAPI) {
|
|
|
247
276
|
},
|
|
248
277
|
});
|
|
249
278
|
|
|
279
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Context window scopes]]
|
|
280
|
+
const tokenWindows = new ContextTokens(getAgentDir(), () =>
|
|
281
|
+
fastOverlay.providerId(),
|
|
282
|
+
);
|
|
283
|
+
const applyContextTokens = (ctx: ExtensionContext) => {
|
|
284
|
+
const session = asTokensSession(stickyDefaults.session(ctx.sessionManager));
|
|
285
|
+
if (!session) return undefined;
|
|
286
|
+
const applied = tokenWindows.apply(session, ctx.modelRegistry);
|
|
287
|
+
activeTui?.requestRender();
|
|
288
|
+
return applied && { session, ...applied };
|
|
289
|
+
};
|
|
290
|
+
pi.registerCommand?.("tokens", {
|
|
291
|
+
description:
|
|
292
|
+
"Show or set the OpenAI context window: /tokens [max|default] [global]",
|
|
293
|
+
getArgumentCompletions: tokensArgumentCompletions,
|
|
294
|
+
handler: async (args, ctx) => {
|
|
295
|
+
const request = parseTokensArgs(args);
|
|
296
|
+
if (!request) {
|
|
297
|
+
ctx.ui.notify(TOKENS_USAGE, "error");
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (!asTokensSession(stickyDefaults.session(ctx.sessionManager))) {
|
|
301
|
+
ctx.ui.notify(
|
|
302
|
+
"/tokens is unavailable: this Pi version does not expose the live session model.",
|
|
303
|
+
"error",
|
|
304
|
+
);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const { mode, global } = request;
|
|
308
|
+
if (mode && global) {
|
|
309
|
+
try {
|
|
310
|
+
tokenWindows.setGlobalMode(mode);
|
|
311
|
+
} catch (error) {
|
|
312
|
+
const message =
|
|
313
|
+
error instanceof Error ? error.message : String(error);
|
|
314
|
+
ctx.ui.notify(`Failed to save context window: ${message}`, "error");
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
// This session drops its own choice and follows the new global one.
|
|
318
|
+
if (tokenWindows.sessionMode(ctx.sessionManager.getBranch())) {
|
|
319
|
+
pi.appendEntry(CONTEXT_TOKENS_ENTRY, { mode: null });
|
|
320
|
+
}
|
|
321
|
+
} else if (mode) {
|
|
322
|
+
pi.appendEntry(CONTEXT_TOKENS_ENTRY, { mode });
|
|
323
|
+
}
|
|
324
|
+
const modelId = ctx.model?.id ?? "No model";
|
|
325
|
+
const applied = applyContextTokens(ctx);
|
|
326
|
+
if (!applied) {
|
|
327
|
+
const saved = mode
|
|
328
|
+
? `Context window ${mode} saved ${global ? "for all sessions" : "for this session"}, but `
|
|
329
|
+
: "";
|
|
330
|
+
ctx.ui.notify(
|
|
331
|
+
`${saved}${modelId} has no larger window: /tokens applies to OpenAI models on openai, openai-codex, and CLIProxyAPI whose maximum exceeds the default.`,
|
|
332
|
+
mode ? "warning" : "info",
|
|
333
|
+
);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const branch = ctx.sessionManager.getBranch();
|
|
337
|
+
const sessionMode = tokenWindows.sessionMode(branch);
|
|
338
|
+
const globalMode = tokenWindows.globalMode();
|
|
339
|
+
const notice = tokensNotice({
|
|
340
|
+
modelId,
|
|
341
|
+
mode:
|
|
342
|
+
mode ??
|
|
343
|
+
(sessionMode
|
|
344
|
+
? `${sessionMode}, set for this session`
|
|
345
|
+
: globalMode
|
|
346
|
+
? `${globalMode}, set globally`
|
|
347
|
+
: "default"),
|
|
348
|
+
scope: mode ? (global ? "global" : "session") : "status",
|
|
349
|
+
window: applied.window,
|
|
350
|
+
limits: applied.limits,
|
|
351
|
+
compaction: applied.session.settingsManager.getCompactionSettings(
|
|
352
|
+
applied.session.agent.state.model,
|
|
353
|
+
),
|
|
354
|
+
contextTokens: ctx.getContextUsage()?.tokens,
|
|
355
|
+
});
|
|
356
|
+
ctx.ui.notify(notice.message, notice.level);
|
|
357
|
+
},
|
|
358
|
+
});
|
|
359
|
+
// Pi reads the session model's window live in every compaction and
|
|
360
|
+
// overflow check. The session_start guard windows every model write; a
|
|
361
|
+
// global choice made in another process lands before each check instead:
|
|
362
|
+
// prompt-time checks run after `input`, continuation and post-run checks
|
|
363
|
+
// after `turn_end`.
|
|
364
|
+
let removeTokensGuard: (() => void) | undefined;
|
|
365
|
+
pi.on("input", (_event, ctx) => {
|
|
366
|
+
applyContextTokens(ctx);
|
|
367
|
+
});
|
|
368
|
+
pi.on("turn_end", (_event, ctx) => {
|
|
369
|
+
applyContextTokens(ctx);
|
|
370
|
+
});
|
|
371
|
+
|
|
250
372
|
let removeFooterColors: (() => void) | undefined;
|
|
251
373
|
let removeKeybindings: (() => void) | undefined;
|
|
252
374
|
let removeWheelScroll: (() => void) | undefined;
|
|
@@ -338,9 +460,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
338
460
|
};
|
|
339
461
|
|
|
340
462
|
pi.registerCommand?.(RESTORE_MODEL_COMMAND, {
|
|
341
|
-
description: "Internal: restore
|
|
463
|
+
description: "Internal: restore model, thinking, and Fast after /clear",
|
|
342
464
|
handler: async (args, ctx) => {
|
|
343
|
-
const reference =
|
|
465
|
+
const reference = decodeClearSettings(args);
|
|
344
466
|
if (!reference) {
|
|
345
467
|
ctx.ui.notify("Could not restore model after /clear", "error");
|
|
346
468
|
return;
|
|
@@ -351,16 +473,38 @@ export default function (pi: ExtensionAPI) {
|
|
|
351
473
|
`Could not restore model ${reference.provider}/${reference.id}`,
|
|
352
474
|
"error",
|
|
353
475
|
);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
// Model selection reapplies defaults, so restore thinking afterwards.
|
|
479
|
+
pi.setThinkingLevel(reference.thinkingLevel);
|
|
480
|
+
if (fastOverlay.isSessionEnabled() !== reference.sessionFast)
|
|
481
|
+
fastOverlay.toggleSession();
|
|
482
|
+
if (reference.contextTokens) {
|
|
483
|
+
pi.appendEntry(CONTEXT_TOKENS_ENTRY, { mode: reference.contextTokens });
|
|
354
484
|
}
|
|
485
|
+
// Reselecting the same model writes nothing, so apply the new entry.
|
|
486
|
+
applyContextTokens(ctx);
|
|
487
|
+
activeTui?.requestRender();
|
|
355
488
|
},
|
|
356
489
|
});
|
|
357
490
|
|
|
358
491
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Model-preserving clear]]
|
|
359
492
|
pi.registerCommand?.("clear", {
|
|
360
|
-
description:
|
|
493
|
+
description:
|
|
494
|
+
"Start a new session with the current model, thinking, Fast, and context window",
|
|
361
495
|
handler: async (_args, ctx) => {
|
|
362
496
|
const restoreCommand = ctx.model
|
|
363
|
-
? `/${RESTORE_MODEL_COMMAND} ${
|
|
497
|
+
? `/${RESTORE_MODEL_COMMAND} ${encodeURIComponent(
|
|
498
|
+
JSON.stringify({
|
|
499
|
+
provider: ctx.model.provider,
|
|
500
|
+
id: ctx.model.id,
|
|
501
|
+
thinkingLevel: pi.getThinkingLevel(),
|
|
502
|
+
sessionFast: fastOverlay.isSessionEnabled(),
|
|
503
|
+
contextTokens: tokenWindows.sessionMode(
|
|
504
|
+
ctx.sessionManager.getBranch(),
|
|
505
|
+
),
|
|
506
|
+
} satisfies ClearSettings),
|
|
507
|
+
)}`
|
|
364
508
|
: undefined;
|
|
365
509
|
await ctx.newSession(
|
|
366
510
|
restoreCommand
|
|
@@ -450,11 +594,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
450
594
|
});
|
|
451
595
|
|
|
452
596
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Model image context]]
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
const messages = pinSkillContext(
|
|
597
|
+
pi.on("context", async (event, ctx) => {
|
|
598
|
+
const messages = await skills.transform(
|
|
456
599
|
omitPriorTurnImages(event.messages),
|
|
457
|
-
ctx
|
|
600
|
+
ctx,
|
|
458
601
|
);
|
|
459
602
|
if (messages !== event.messages) return { messages };
|
|
460
603
|
});
|
|
@@ -466,13 +609,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
466
609
|
event.systemPromptOptions?.selectedTools ?? pi.getActiveTools?.() ?? [];
|
|
467
610
|
applyProactiveDelegation(
|
|
468
611
|
event.systemPromptOptions,
|
|
469
|
-
|
|
612
|
+
// pi-subagents 0.71+ starts sessions with only its loader active.
|
|
613
|
+
tools.includes("subagent") || tools.includes("subagents_enable"),
|
|
470
614
|
ctx.scopedModels.map((scoped) => scoped.model),
|
|
471
615
|
);
|
|
472
616
|
});
|
|
473
617
|
|
|
474
618
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Automatic session title]]
|
|
475
619
|
pi.on("before_agent_start", (event) => {
|
|
620
|
+
skills.prepare(event);
|
|
476
621
|
if (!sessionTitlePending || pi.getSessionName?.()) {
|
|
477
622
|
sessionTitlePending = false;
|
|
478
623
|
return;
|
|
@@ -586,6 +731,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
586
731
|
|
|
587
732
|
pi.on("session_shutdown", () => {
|
|
588
733
|
removeFastSessionList();
|
|
734
|
+
removeTokensGuard?.();
|
|
735
|
+
removeTokensGuard = undefined;
|
|
589
736
|
stickyDefaults.restore();
|
|
590
737
|
removeKeybindings?.();
|
|
591
738
|
removeKeybindings = undefined;
|
|
@@ -629,11 +776,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
629
776
|
restoreRequest = undefined;
|
|
630
777
|
sessionTitlePending = false;
|
|
631
778
|
promptDisplayHost.dispose();
|
|
779
|
+
skills.stop();
|
|
632
780
|
});
|
|
633
781
|
|
|
634
782
|
pi.on("session_start", async (_event, ctx) => {
|
|
635
783
|
stickyDefaults.activate(ctx.sessionManager);
|
|
636
|
-
|
|
784
|
+
skills.start(ctx);
|
|
785
|
+
// /clear restores its captured session Fast after startup completes.
|
|
637
786
|
fastOverlay.resetSession();
|
|
638
787
|
promptDisplayHost.activate(ctx.sessionManager);
|
|
639
788
|
sessionTitlePending =
|
|
@@ -754,7 +903,12 @@ export default function (pi: ExtensionAPI) {
|
|
|
754
903
|
removeJumpToBottom = installJumpToBottom(editor, tui);
|
|
755
904
|
removePromptJump?.();
|
|
756
905
|
settings?.dispose();
|
|
757
|
-
settings = installSettings(tui, editor, getAgentDir())
|
|
906
|
+
settings = installSettings(tui, editor, getAgentDir(), (error) =>
|
|
907
|
+
ctx.ui.notify(
|
|
908
|
+
`Could not save proper-base setting: ${error.message}`,
|
|
909
|
+
"error",
|
|
910
|
+
),
|
|
911
|
+
);
|
|
758
912
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Prompt mouse clicks]]
|
|
759
913
|
removeEditorMouseGuard?.();
|
|
760
914
|
removeEditorMouseGuard = installEditorMouseGuard(
|
|
@@ -801,6 +955,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
801
955
|
},
|
|
802
956
|
);
|
|
803
957
|
installModelAutocompleteSubmit(editor, keybindings);
|
|
958
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Context window scopes]]
|
|
959
|
+
installTokensScopeCompletion(editor, keybindings);
|
|
804
960
|
removePromptClear?.();
|
|
805
961
|
removePromptClear = installPromptClear(editor, tui, keybindings, ctx);
|
|
806
962
|
historySearch = installReverseHistorySearch(
|
|
@@ -825,6 +981,14 @@ export default function (pi: ExtensionAPI) {
|
|
|
825
981
|
factory[WRAPPED] = base ?? null;
|
|
826
982
|
|
|
827
983
|
ctx.ui.setEditorComponent(factory);
|
|
984
|
+
// Window the restored model now and every model written later.
|
|
985
|
+
removeTokensGuard?.();
|
|
986
|
+
const tokensSession = asTokensSession(
|
|
987
|
+
stickyDefaults.session(ctx.sessionManager),
|
|
988
|
+
);
|
|
989
|
+
removeTokensGuard =
|
|
990
|
+
tokensSession && tokenWindows.guard(tokensSession, ctx.modelRegistry);
|
|
991
|
+
applyContextTokens(ctx);
|
|
828
992
|
});
|
|
829
993
|
// Base setup and cleanup must register before the updater's lifecycle hooks.
|
|
830
994
|
registerAutoUpdates(pi);
|
package/package.json
CHANGED
|
@@ -36,7 +36,7 @@ const MODEL_ORDER = new Intl.Collator("en", {
|
|
|
36
36
|
* list nor the per-model subset, and Pi clamps an unsupported level when it
|
|
37
37
|
* applies one, so restating the full list here costs nothing.
|
|
38
38
|
*/
|
|
39
|
-
const THINKING_LEVELS = [
|
|
39
|
+
export const THINKING_LEVELS = [
|
|
40
40
|
"off",
|
|
41
41
|
"minimal",
|
|
42
42
|
"low",
|