pi-ext
bugabingas pi extensions
owned by bugabinga
mode: add minimal picker and status
de8f57297e509729bc264de35202243607aa96b9
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (6)
extensions/mode/README.md
Modified15 15 - `/mode status` — same as `/mode`.
16 16 - `/mode list` — list available modes.
17 17 - `/mode <id>` — switch to a mode.
18 +- `/mode select` — open an interactive mode picker.
19 +- `/mode next` — cycle to the next mode.
18 20 - `/mode off` — turn mode off (notifies `no mode active` if no mode is active).
19 21 - `/mode normal` — alias for `/mode off`.
20 22 - `/mode none` — alias for `/mode off`.
21 23 - `/mode reload-config` — reload mode files and report count/warnings.
27 +- `ctrl+shift+m` — open the interactive mode picker.
73 79 - Replaces the built-in header with a themed banner above the chat.
80 +- Shows `mode:<id>` in the footer status.
74 81 - Appends its Markdown body and a generated `<mode_kernel>` to the system prompt for each agent turn.
75 82 - May switch tools, model, and thinking level on enter.
extensions/mode/V1.md
Modified245 245 /mode status same as /mode
246 246 /mode list show available modes
247 247 /mode <
id> switch active mode
248 +/mode select open an interactive mode picker
249 +/mode next cycle to the next mode
248 250 /mode off turn mode off (notify "no mode active" if already off)
249 251 /mode normal alias for /mode off
250 252 /mode none alias for /mode off
278 280 `ctx.ui.setHeader(undefined)` clears the custom header and restores the built-in (logo + onboarding) on `/mode off` and `session_start`.
280 -The header is the only persistent UI signal of mode state.
281 -`notify` is reserved for warnings (unknown tool, model not found, malformed ref, parse error, unknown mode id).
282 +The header and footer status are the persistent UI signals of mode state.
283 +The footer status shows `mode:<id>` and clears on `/mode off`, `session_start`, and `session_shutdown`.
284 +`ctrl+shift+m` opens the interactive mode picker.
285 +`notify` is reserved for warnings (unknown tool, model not found, malformed ref, parse error, unknown mode id) and empty-picker status.
extensions/mode/V3.md
Modified63 63 Not before tool policy/tracking design is done.
65 -##
Interactive switcher 65 +##
Rich interactive switcher 67 -`/mode` interactive fuzzy switcher:
67 +V1 has a minimal picker via `/mode select` and `ctrl+shift+m`.
69 +Future rich picker ideas:
71 74 - render description
72 -- maybe preview model/thinking/tools
75 +- preview model/thinking/tools
extensions/mode/__tests__/coverage.test.ts
Modified 14 14 const events
= new Map<
string, (event
: any, ctx
: any)
=> unknown>();
15 15 const notes
: Array<[
string,
string]>
= [];
16 16 const headers
: unknown[]
= [];
17 + const statuses
: unknown[][]
= [];
18 + const shortcuts
= new Map<
string, {
handler: (ctx
: any)
=> Promise<
void> }>();
17 19 const activeTools
: string[][]
= [];
20 + const selections
: Array<
string | undefined>
= [];
18 21 let setModelResult
= true;
19 22 let modelFindResult
: unknown;
20 23 let thinking
: string | undefined;
27 30 notify: (message
: string, severity
: string)
=> 28 31 notes.
push([message, severity]),
32 + select:
async ()
=> selections.
shift(),
29 33 setHeader: (
...args
: unknown[])
=> headers.
push(args),
34 + setStatus: (
...args
: unknown[])
=> statuses.
push(args),
36 + fg: (_color
: string, text
: string)
=> text,
39 47 command: {
handler: (args
: string, ctx
: any)
=> Promise<void> },
40 48 )
=> commands.
set(name, command),
51 + shortcut: {
handler: (ctx
: any)
=> Promise<void> },
52 + )
=> shortcuts.
set(key, shortcut),
41 53 setActiveTools: (tools
: string[])
=> activeTools.
push(tools),
42 54 setModel:
async ()
=> setModelResult,
43 55 setThinkingLevel: (level
: string)
=> {
53 65 modelFindResult: ()
=> modelFindResult,
67 + select: (choice
: string | undefined)
=> selections.
push(choice),
68 + shortcut: (key
: string)
=> shortcuts.
get(key)?.
handler(ctx),
55 71 setModelFindResult: (model
: unknown)
=> {
56 72 modelFindResult
= model;
276 292 await f.
command(
"plan");
277 293 expect(f.activeTools.
at(
-1)).
toEqual([
"read",
"grep"]);
278 294 expect(
typeof f.
headers.
at(
-1)?.[
0]).
toBe(
"function");
295 + expect(f.statuses.
at(
-1)).
toEqual([
"mode",
"mode:plan"]);
280 297 f.events.
get(
"before_agent_start")?.({ systemPrompt:
"BASE" }, f.ctx),
281 298 ).
toEqual({ systemPrompt: expect.
stringContaining(
"active_mode: plan") });
282 299 await f.
command(
"normal");
283 300 expect(f.
headers.
at(
-1)).
toEqual([
undefined]);
301 + expect(f.statuses.
at(
-1)).
toEqual([
"mode",
undefined]);
285 303 f.events.
get(
"before_agent_start")?.({ systemPrompt:
"BASE" }, f.ctx),
287 305 systemPrompt: expect.
stringContaining(
"active_mode: normal"),
289 307 await f.
command(
"plan");
308 + await f.
command(
"next");
309 + expect(f.statuses.
at(
-1)).
toEqual([
"mode",
"mode:plain"]);
310 + f.
select(
"plan — d");
311 + await f.
command(
"select");
312 + expect(f.statuses.
at(
-1)).
toEqual([
"mode",
"mode:plan"]);
313 + expect(f.shortcuts.
has(
"ctrl+shift+m")).
toBe(
true);
315 + await f.
shortcut(
"ctrl+shift+m");
316 + expect(f.statuses.
at(
-1)).
toEqual([
"mode",
"mode:plain"]);
317 + await f.
command(
"plan");
290 318 await f.
command(
"none");
291 319 expect(f.
headers.
at(
-1)).
toEqual([
undefined]);
327 355 await f.
command(
"deep");
328 356 await f.events.
get(
"session_start")?.({ reason:
"reload" }, f.ctx);
329 357 expect(f.
headers.
at(
-1)).
toEqual([
undefined]);
358 + expect(f.statuses.
at(
-1)).
toEqual([
"mode",
undefined]);
330 359 await f.events.
get(
"session_shutdown")?.({}, f.ctx);
331 360 expect(f.
headers.
at(
-1)).
toEqual([
undefined]);
361 + expect(f.statuses.
at(
-1)).
toEqual([
"mode",
undefined]);
extensions/mode/__tests__/unit.test.ts
Modified327 327 on: (name
: string,
handler: (
...args
: unknown[])
=> unknown)
=> 328 328 events.
set(name, handler),
329 329 registerCommand: ()
=> undefined,
330 + registerShortcut: ()
=> undefined,
330 331 setActiveTools: ()
=> undefined,
331 332 setModel:
async ()
=> true,
332 333 setThinkingLevel: ()
=> undefined,
351 352 notify: ()
=> undefined,
352 353 setHeader: (
...args
: unknown[])
=> headers.
push(args),
354 + setStatus: ()
=> undefined,
355 + theme: {
fg: (_color
: string, text
: string)
=> text },
362 365 command: {
handler: (args
: string, ctx
: unknown)
=> Promise<void> },
363 366 )
=> commands.
set(name, command),
367 + registerShortcut: ()
=> undefined,
364 368 setActiveTools: ()
=> undefined,
365 369 setModel:
async ()
=> true,
366 370 setThinkingLevel: ()
=> undefined,
extensions/mode/index.ts
Modified243 243 ctx.ui.
setHeader(
buildHeader(mode));
246 +function publishStatus(ctx
: ExtensionContext, mode
: Mode | null)
: void {
247 + if (
!ctx.hasUI)
return;
249 + ctx.ui.
setStatus(
"mode",
undefined);
252 + const color
: AccentColor = mode.accent
?? "accent";
253 + ctx.ui.
setStatus(
"mode", ctx.ui.theme.
fg(color,
`mode:${mode.id}`));
246 256 export default function modeExtension(pi
: ExtensionAPI)
: void {
247 257 const state
: ModeRuntimeState = {
248 258 activeModeId:
null,
303 313 state.neutralize
= false;
304 314 await applyMode(mode, ctx);
305 315 publishHeader(ctx, mode);
316 + publishStatus(ctx, mode);
319 + async function selectMode(ctx
: ExtensionContext)
: Promise<
void> {
320 + if (
!ctx.hasUI)
return;
321 + await reloadConfig(ctx);
322 + const modes
= [
...state.modes.
values()];
323 + if (modes.
length === 0) {
324 + ctx.ui.
notify(
"no modes available",
"info");
327 + const labels
= new Map(modes.
map((mode)
=> [
modeLine(mode), mode.
id]));
328 + const choice
= await ctx.ui.
select(
"Select mode", [
...labels.
keys()]);
329 + if (
!choice)
return;
330 + const id
= labels.
get(choice);
331 + if (id)
await switchMode(id, ctx);
334 + async function cycleMode(ctx
: ExtensionContext)
: Promise<
void> {
335 + await reloadConfig(ctx);
336 + const ids
= [
...state.modes.
keys()];
337 + if (ids.
length === 0) {
338 + ctx.ui.
notify(
"no modes available",
"info");
341 + const currentIndex
= state.activeModeId
342 + ? ids.
indexOf(state.activeModeId)
344 + const next
= ids[(currentIndex
+ 1)
% ids.
length];
346 + await switchMode(next, ctx);
308 349 function modeOff(ctx
: ExtensionContext)
: void {
313 354 state.activeModeId
= null;
314 355 state.neutralize
= true;
315 356 publishHeader(ctx,
null);
357 + publishStatus(ctx,
null);
318 360 function showStatus(ctx
: ExtensionContext)
: void {
396 + if (arg
=== "select") {
397 + await selectMode(ctx);
400 + if (arg
=== "next") {
401 + await cycleMode(ctx);
354 404 if (arg
=== "off" || arg
=== "normal" || arg
=== "none") {
412 + pi.
registerShortcut(
"ctrl+shift+m", {
413 + description:
"Select mode",
414 + handler:
async (ctx)
=> selectMode(ctx),
362 417 pi.
on(
"before_agent_start", (event)
=> {
363 418 const mode
= state.activeModeId
364 419 ? state.modes.
get(state.activeModeId)
379 434 state.activeModeId
= null;
380 435 state.neutralize
= false;
381 436 publishHeader(ctx,
null);
437 + publishStatus(ctx,
null);
384 - pi.
on(
"session_shutdown", (_event, ctx)
=> publishHeader(ctx,
null));
440 + pi.
on(
"session_shutdown", (_event, ctx)
=> {
441 + publishHeader(ctx,
null);
442 + publishStatus(ctx,
null);
387 446 export const __modeTest
= {