pi-ext
bugabingas pi extensions
owned by bugabinga
feat(masks): switch model and thinking together
e7462aec3a87b0e3dbdf8c6073db78b3d97ed15b
Oliver Jan Krylow <oliver@bugabinga.net> ·
SSH signature · not checked
Browse filesCompare with HEAD
Changed files (7)
82 82 | `goal` | Persistent standing-goal loop for Pi sessions. | [README](extensions/goal/README.md) |
83 83 | `leaks` | Provider-bound secret redaction for Pi. | [README](extensions/leaks/README.md) |
84 84 | `llmiterate` | Manual editor-save bridge from comment markers to background Pi edits. | [README](extensions/llmiterate/README.md) |
85 +| `masks` | Named model and thinking combinations for Pi. | [README](extensions/masks/README.md) |
85 86 | `model-info` | Model and thinking telemetry for Pi. | [README](extensions/model-info/README.md) |
86 87 | `neko` | Tiny animated editor cat for Pi. Pointless. Correct. | [README](extensions/neko/README.md) |
87 88 | `notes` | Session-backed pending prompt notes for Pi. | [README](extensions/notes/README.md) |
extensions/masks/CHANGELOG.md
Addednew file mode 100644 5 +- Add `/masks`, configurable picker shortcut, and per-mask shortcuts
6 +- Apply configured model and thinking level together
7 +- Validate global `masks` settings and report invalid entries
extensions/masks/README.md
Addednew file mode 100644 3 +Switch model and thinking level together.
7 +This extension is loaded through the root pi-ext package.
8 +See [root README](../../README.md).
10 +##
Commands / tools / settings 14 +- `/masks` opens the mask picker.
15 +- `/masks <name>` equips one mask directly.
22 +- `session_start` reports invalid mask settings.
29 + "pickerShortcut":
"alt+m",
33 + "model":
"openai-codex/gpt-5.6-sol",
34 + "thinkingLevel":
"high",
42 +`masks.pickerShortcut` is optional and defaults to `alt+m`.
43 +`masks.items` is an ordered array.
44 +Each item requires a unique `name`, a `provider/model`, and a `thinkingLevel`.
45 +`shortcut` is optional and must not collide with the picker or another mask.
46 +Thinking levels are `off`, `minimal`, `low`, `medium`, `high`, `xhigh`, and `max`.
47 +Settings are global because masks describe personal model access and bindings.
48 +Run `/reload` after changing them.
52 +The picker shows every configured mask in settings order.
53 +A mask changes the model first, then applies its thinking level.
54 +Missing models or credentials leave the thinking level unchanged and report an error.
55 +Successful switches add no status because Pi's existing footer shows model and thinking.
extensions/masks/__tests__/harness.test.ts
Addednew file mode 100644 1 +import { mkdirSync, mkdtempSync, rmSync, writeFileSync }
from "node:fs";
2 +import { tmpdir }
from "node:os";
3 +import { join }
from "node:path";
4 +import { fileURLToPath }
from "node:url";
5 +import { afterEach, describe, expect, it }
from "vitest";
6 +import { createTestSession, type TestSession }
from "../../../test/harness";
7 +import { __test }
from "../index.ts";
9 +let root
: string | undefined;
10 +let t
: TestSession | undefined;
12 +function extensionPath()
: string {
13 + return fileURLToPath(
new URL(
"../index.ts",
import.
meta.url));
16 +function writeSettings(settings
: Record<
string,
unknown>)
: string {
17 + root = mkdtempSync(
join(
tmpdir(),
"masks-test-"));
18 + const agentDir
= join(
root,
".test-home",
".pi",
"agent");
19 + mkdirSync(agentDir, { recursive:
true });
20 + writeFileSync(
join(agentDir,
"settings.json"),
JSON.
stringify(settings));
24 +function extension() {
25 + if (
!t)
throw new Error(
"missing test session");
26 + return t.session.extensionRunner.extensions[
0];
30 + if (
!t)
throw new Error(
"missing test session");
31 + return t.session.extensionRunner.
createCommandContext();
37 + if (
root)
rmSync(
root, { recursive:
true, force:
true });
41 +describe(
"masks pi-test-harness", ()
=> {
42 + it(
"switches complete masks from direct shortcut, picker, and command",
async ()
=> {
43 + const cwd
= writeSettings({
45 + pickerShortcut:
"alt+m",
49 + model:
"openai/gpt-4o",
50 + thinkingLevel:
"low",
55 + model:
"openai/gpt-4o",
56 + thinkingLevel:
"high",
61 + model:
"absent/nope",
62 + thinkingLevel:
"max",
68 + t
= await createTestSession({
70 + extensions: [
extensionPath()],
71 + mockUI: { select:
1 },
74 + const model
= context().modelRegistry.
find(
"openai",
"gpt-4o");
75 + if (
!model)
throw new Error(
"missing harness model");
76 + model.reasoning
= true;
78 + expect([
...extension().commands.
keys()]).
toEqual([
"masks"]);
79 + expect([
...extension().shortcuts.
keys()]).
toEqual([
86 + await extension().shortcuts.
get(
"alt+1").
handler(
context());
87 + expect(t.session.model.provider).
toBe(
"openai");
88 + expect(t.session.model.
id).
toBe(
"gpt-4o");
89 + expect(t.session.thinkingLevel).
toBe(
"low");
91 + await extension().shortcuts.
get(
"alt+m").
handler(
context());
92 + expect(t.session.thinkingLevel).
toBe(
"high");
93 + expect(t.events.
uiCallsFor(
"select")[
0].args).
toEqual([
96 + "low openai/gpt-4o:low",
97 + "high openai/gpt-4o:high",
98 + "missing absent/nope:max",
102 + await extension().commands.
get(
"masks").
handler(
"low",
context());
103 + expect(t.session.thinkingLevel).
toBe(
"low");
105 + await extension().shortcuts.
get(
"alt+3").
handler(
context());
106 + expect(t.session.thinkingLevel).
toBe(
"low");
107 + expect(t.events.
uiCallsFor(
"notify").
at(
-1)?.args).
toEqual([
108 + 'Mask "missing": model absent/nope not found',
113 + it(
"validates schema while preserving usable picker entries", ()
=> {
114 + const parsed
= __test.
parseMasksSettings({
115 + pickerShortcut:
"alt+m",
119 + model:
"provider/a",
120 + thinkingLevel:
"high",
125 + model:
"provider/b",
126 + thinkingLevel:
"low",
130 + model:
"provider/b",
131 + thinkingLevel:
"low",
134 + { name:
"bad", model:
"missing-provider", thinkingLevel:
"huge" },
138 + expect(parsed.config).
toEqual({
139 + pickerShortcut:
"alt+m",
143 + model:
"provider/a",
144 + thinkingLevel:
"high",
149 + model:
"provider/b",
150 + thinkingLevel:
"low",
151 + shortcut:
undefined,
155 + expect(parsed.errors).
toEqual([
156 + 'masks.items[1].name duplicates "a"',
157 + 'masks.items[2].shortcut duplicates "alt+1"',
158 + "masks.items[3].model must be provider/model",
159 + "masks.items[3].thinkingLevel is invalid",
163 + it(
"keeps a model id containing slashes", ()
=> {
164 + expect(__test.
splitModelRef(
"provider/org/model")).
toEqual([
extensions/masks/index.ts
Addednew file mode 100644 4 +}
from "@earendil-works/pi-coding-agent";
5 +import { SettingsManager }
from "@earendil-works/pi-coding-agent";
7 +const SETTINGS_KEY
= "masks";
8 +const DEFAULT_PICKER_SHORTCUT
= "alt+m";
9 +const THINKING_LEVELS
= [
19 +type ThinkingLevel = (
typeof THINKING_LEVELS)[
number];
20 +type Shortcut = Parameters<
ExtensionAPI[
"registerShortcut"]>[
0];
25 + thinkingLevel
: ThinkingLevel;
29 +interface MasksConfig {
30 + pickerShortcut
: string;
34 +interface ParsedMasks {
35 + config
: MasksConfig;
39 +function isRecord(value
: unknown)
: value
is Record<
string,
unknown> {
40 + return typeof value
=== "object" && value
!== null && !Array.
isArray(value);
43 +function nonEmptyString(value
: unknown)
: string | undefined {
44 + return typeof value
=== "string" && value.
trim()
? value.
trim()
: undefined;
47 +function isThinkingLevel(value
: unknown)
: value
is ThinkingLevel {
48 + return THINKING_LEVELS.
includes(value
as ThinkingLevel);
51 +function parseMasksSettings(value
: unknown)
: ParsedMasks {
52 + const errors
: string[]
= [];
53 + const config
: MasksConfig = {
54 + pickerShortcut: DEFAULT_PICKER_SHORTCUT,
57 + if (value
=== undefined)
return { config, errors };
58 + if (
!isRecord(value)) {
59 + return { config, errors: [
"masks must be an object"] };
62 + if (value.pickerShortcut
!== undefined) {
63 + const shortcut
= nonEmptyString(value.pickerShortcut);
64 + if (shortcut) config.pickerShortcut
= shortcut;
65 + else errors.
push(
"masks.pickerShortcut must be a non-empty string");
68 + if (value.items
=== undefined)
return { config, errors };
69 + if (
!Array.
isArray(value.items)) {
70 + errors.
push(
"masks.items must be an array");
71 + return { config, errors };
74 + const names
= new Set<
string>();
75 + const shortcuts
= new Set<
string>([config.pickerShortcut]);
76 + for (
const [index, valueItem]
of value.items.
entries()) {
77 + const path
= `masks.items[${index}]`;
78 + if (
!isRecord(valueItem)) {
79 + errors.
push(
`${path} must be an object`);
83 + const name
= nonEmptyString(valueItem.
name);
84 + const model
= nonEmptyString(valueItem.model);
85 + const thinkingLevel
= valueItem.thinkingLevel;
88 + errors.
push(
`${path}.name must be a non-empty string`);
90 + }
else if (names.
has(name)) {
91 + errors.
push(
`${path}.name duplicates "${name}"`);
96 + model.
startsWith(
"/")
|| 97 + model.
endsWith(
"/")
|| 98 + !model.
includes(
"/")
100 + errors.
push(
`${path}.model must be provider/model`);
103 + if (
!isThinkingLevel(thinkingLevel)) {
104 + errors.
push(
`${path}.thinkingLevel is invalid`);
107 + if (
!valid
|| !name
|| !model
|| !isThinkingLevel(thinkingLevel))
continue;
109 + let shortcut
: string | undefined;
110 + if (valueItem.shortcut
!== undefined) {
111 + shortcut
= nonEmptyString(valueItem.shortcut);
113 + errors.
push(
`${path}.shortcut must be a non-empty string`);
114 + }
else if (shortcuts.
has(shortcut)) {
115 + errors.
push(
`${path}.shortcut duplicates "${shortcut}"`);
116 + shortcut
= undefined;
121 + if (shortcut) shortcuts.
add(shortcut);
122 + config.items.
push({ name, model, thinkingLevel, shortcut });
124 + return { config, errors };
127 +function readMasksSettings()
: ParsedMasks {
128 + const settings
= SettingsManager.
create(
130 + ).
getGlobalSettings()
as Record<
string,
unknown>;
131 + return parseMasksSettings(settings[SETTINGS_KEY]);
134 +function splitModelRef(model
: string)
: [
provider:
string,
modelId:
string] {
135 + const separator
= model.
indexOf(
"/");
136 + return [model.
slice(
0, separator), model.
slice(separator
+ 1)];
139 +async function applyMask(
142 + ctx
: ExtensionContext,
143 +)
: Promise<
boolean> {
144 + const [provider, modelId]
= splitModelRef(mask.model);
145 + const model
= ctx.modelRegistry.
find(provider, modelId);
148 + `Mask "${mask.name}": model ${mask.model} not found`,
153 + if (
!(
await pi.
setModel(model))) {
155 + `Mask "${mask.name}": no credentials for ${mask.model}`,
160 + pi.
setThinkingLevel(mask.thinkingLevel);
164 +export default function masksExtension(pi
: ExtensionAPI)
: void {
165 + const { config, errors }
= readMasksSettings();
167 + async function chooseMask(ctx
: ExtensionContext)
: Promise<
void> {
168 + if (config.items.
length === 0) {
169 + ctx.ui.
notify(
"No masks configured in settings.json",
"warning");
172 + const options
= config.items.
map(
173 + (mask)
=> `${mask.name} ${mask.model}:${mask.thinkingLevel}`,
175 + const selected
= await ctx.ui.
select(
"Masks", options);
176 + if (selected
=== undefined)
return;
177 + const index
= options.
indexOf(selected);
178 + if (index
>= 0)
await applyMask(pi, config.items[index], ctx);
181 + pi.
registerShortcut(config.pickerShortcut
as Shortcut, {
182 + description:
"Choose model and thinking mask",
183 + handler: chooseMask,
185 + for (
const mask
of config.items) {
186 + if (
!mask.shortcut)
continue;
187 + pi.
registerShortcut(mask.shortcut
as Shortcut, {
188 + description:
`Equip mask ${mask.name}`,
189 + handler:
async (ctx)
=> {
190 + await applyMask(pi, mask, ctx);
195 + pi.
registerCommand(
"masks", {
196 + description:
"Switch model and thinking mask",
197 + handler:
async (args, ctx)
=> {
198 + const name
= args?.
trim();
200 + await chooseMask(ctx);
203 + const mask
= config.items.
find((item)
=> item.
name === name);
205 + ctx.ui.
notify(
`Unknown mask "${name}"`,
"error");
208 + await applyMask(pi, mask, ctx);
212 + pi.
on(
"session_start", (_event, ctx)
=> {
213 + for (
const error
of errors) ctx.ui.
notify(error,
"warning");
217 +export const __test
= { applyMask, parseMasksSettings, splitModelRef };
extensions/masks/package.json
Addednew file mode 100644 2 + "name":
"@bugabinga/pi-ext-masks",
5 + "peerDependencies": {
6 + "@earendil-works/pi-coding-agent":
"*" 9 + "description":
"Named model and thinking combinations for Pi.",
14 + "devDependencies": {
15 + "@marcfargas/pi-test-harness":
"^0.6.1",
package-lock.json
Modified 309 309 "@earendil-works/pi-tui":
"*" 312 + "extensions/masks": {
313 + "name":
"@bugabinga/pi-ext-masks",
315 + "devDependencies": {
316 + "@marcfargas/pi-test-harness":
"^0.6.1",
319 + "peerDependencies": {
320 + "@earendil-works/pi-coding-agent":
"*" 312 323 "extensions/model-info": {
313 324 "name":
"@bugabinga/pi-ext-model-info",
314 325 "license":
"MIT",
1503 1514 "resolved":
"extensions/llmiterate",
1517 + "node_modules/@bugabinga/pi-ext-masks": {
1518 + "resolved":
"extensions/masks",
1506 1521 "node_modules/@bugabinga/pi-ext-model-info": {
1507 1522 "resolved":
"extensions/model-info",