import {SessionManager, type ExtensionAPI} from "@earendil-works/pi-coding-agent";
import {Type} from "typebox";
export default function (pi: ExtensionAPI) {
pi.registerFlag("gallery-view", {description: "Append a gallery note to model prompts", type: "boolean", default: false});
const duringLoad = pi.getFlag("gallery-view");
pi.on("before_agent_start", event => {
if (pi.getFlag("gallery-view")) return {systemPrompt: event.systemPrompt + "\nGALLERY FIXTURE VIEW ACTIVE."};
});
pi.registerCommand("gallery-flag", {
description: "Show the fixture flag seen at load and after parsing",
handler: async (_args, ctx) => ctx.ui.notify("FLAG " + duringLoad + ":" + pi.getFlag("gallery-view"), "info"),
});
pi.registerCommand("gallery-count", {
description: "Count session entries",
handler: async (_args, ctx) => ctx.ui.notify("GALLERY ENTRIES " + ctx.sessionManager.getEntries().length, "info"),
});
pi.registerCommand("gallery-pick", {
description: "Choose a color label",
getArgumentCompletions(prefix) {
const list = ["amber", "azure", "pearl"].filter(color => color.startsWith(prefix))
.map(color => ({value: color, label: color}));
return list.length ? list : null;
},
handler: async (args, ctx) => ctx.ui.notify(args ? "GALLERY PICK " + args.toUpperCase() : "PICK A COLOR LABEL", "info"),
});
pi.registerShortcut("alt+9", {
description: "Display the local key marker",
handler: async ctx => ctx.ui.notify("GALLERY HOTKEY OBSERVED", "info"),
});
pi.registerCommand("gallery-code", {
description: "Report the first extension",
handler: async (_args, ctx) => ctx.ui.notify("GALLERY CODE A", "info"),
});
pi.registerCommand("gallery-signal", {
description: "Emit a synthetic label event",
handler: async () => pi.events.emit("gallery:label", {name: "Atlas"}),
});
pi.registerCommand("gallery-index", {
description: "List the sources of example commands",
handler: async (_args, ctx) => {
const choices = pi.getCommands().filter(command =>
["gallery-index", "gallery-template", "skill:gallery-skill"].includes(command.name));
ctx.ui.notify("INDEX " + JSON.stringify(choices.map(command => ({
name: command.name, description: command.description, source: command.source,
path: command.sourceInfo.path, scope: command.sourceInfo.scope,
}))), "info");
},
});
pi.registerCommand("gallery-source", {
description: "Inspect prompt inputs after idle",
handler: async (_args, ctx) => {
await ctx.waitForIdle();
const input = ctx.getSystemPromptOptions();
ctx.ui.notify("GALLERY SOURCE " + (input.cwd === ctx.cwd) + ":" + ctx.isIdle(), "info");
},
});
pi.registerCommand("gallery-note", {
description: "Add a fixture note for the next prompt",
handler: async () => pi.sendMessage({customType: "gallery-note", content: "GALLERY REFERENCE", display: true}),
});
pi.registerCommand("gallery-run-note", {
description: "Start a run with a fixture note",
handler: async () => pi.sendMessage(
{customType: "gallery-prompt", content: "GALLERY CUSTOM TRIGGER", display: true}, {triggerTurn: true}),
});
pi.registerCommand("gallery-user", {
description: "Send a local synthetic user message",
handler: async () => pi.sendUserMessage("GALLERY SYNTHETIC USER"),
});
pi.registerCommand("gallery-new", {
description: "Start a new temporary session",
handler: async (_args, ctx) => {
const parentSession = ctx.sessionManager.getSessionFile();
const oldId = ctx.sessionManager.getSessionId();
await ctx.newSession({parentSession, withSession: async fresh => {
fresh.ui.notify("GALLERY NEW " + (fresh.sessionManager.getSessionId() !== oldId), "info");
}});
},
});
pi.registerCommand("gallery-switch", {
description: "Select a saved gallery session",
handler: async (_args, ctx) => {
if (!ctx.hasUI) return;
const sessions = await SessionManager.list(ctx.cwd, ctx.sessionManager.getSessionDir());
const paths = sessions.map(session => session.path);
if (!paths.length) return;
const chosen = await ctx.ui.select("Select a gallery session", paths, {timeout: 3000});
if (chosen && paths.includes(chosen)) {
await ctx.switchSession(chosen, {withSession: async fresh => {
fresh.ui.notify("GALLERY SWITCH " + (fresh.sessionManager.getSessionFile() === chosen), "info");
}});
}
},
});
for (const [name, initiallyActive] of [["gallery_card", true], ["gallery_index", false]] as const) {
pi.registerTool({
name, label: name, description: "Returns a synthetic gallery label.",
promptSnippet: "Summarize " + name + " labels.",
defaultActive: initiallyActive,
parameters: Type.Object({}),
async execute() {
return {content: [{type: "text", text: "GALLERY " + name}], details: {}};
},
});
}
pi.registerCommand("gallery-tools", {
description: "Change the tools declared for this exercise",
handler: async (args, ctx) => {
if (args === "add") pi.setActiveTools([...new Set([...pi.getActiveTools(), "gallery_index"])]);
if (args === "card") pi.setActiveTools(["gallery_card"]);
const declared = pi.getAllTools().filter(tool => tool.name.startsWith("gallery_"));
ctx.ui.notify("ACTIVE " + JSON.stringify({active: pi.getActiveTools().filter(name => name.startsWith("gallery_")),
registered: declared.map(tool => tool.name).sort()}), "info");
},
});
}
本例需在 Windows、Node.js 24.22.3、Pi 1.5.0、PyWinPTY 3.1.2、本机回环模型模拟。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、Node.js 24.22.3、Pi 1.5.0、PyWinPTY 3.1.2、本机回环模型模拟。
入口文件:projects/pi-agent-commands-lab/gallery-commands.ts
本地运行步骤快速上手
- 点击上方“保存源码”保存为
gallery-commands.ts,或下载右侧课程工程包。 - 确认本地已准备 Windows、Node.js 24.22.3、Pi 1.5.0、PyWinPTY 3.1.2、本机回环模型模拟。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a268.zip
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a267.zip