// ultra entry point: keep startup limited to command, result rendering, and the auto-enable check. import type { ExtensionAPI, ExtensionCommandContext, } from "@earendil-works/pi-coding-agent"; import { getMarkdownTheme, SettingsManager, } from "@earendil-works/pi-coding-agent"; import { type AutocompleteItem, Markdown } from "@earendil-works/pi-tui"; import type { WorkflowResult } from "./engine.ts"; import { formatUltraResultMarkdown } from "./result.ts"; import { loadUltraSettings } from "./settings.ts"; interface UltraRuntime { activateWorkflowTool(): void; getArgumentCompletions(prefix: string): AutocompleteItem[] | null; handleCommand(argString: string, ctx: ExtensionCommandContext): Promise; } export default function ultraExtension(pi: ExtensionAPI) { pi.registerMessageRenderer( "ultra-result", (message, options, theme) => { if (!message.details) return new Markdown( "ultra result missing details", 0, 0, getMarkdownTheme(), ); return new Markdown( formatUltraResultMarkdown(message.details, options.expanded), 0, 0, getMarkdownTheme(), { color: (text) => theme.fg("customMessageText", text) }, ); }, ); let runtimePromise: Promise | undefined; const runtime = () => (runtimePromise ??= import("./implementation.ts").then( ({ createUltraRuntime }) => createUltraRuntime(pi), )); pi.on("session_start", async (_event, ctx) => { const manager = SettingsManager.create(ctx.cwd); const settings = loadUltraSettings({ global: manager.getGlobalSettings(), project: manager.getProjectSettings(), }); if (settings.enabled && settings.autoEnable) (await runtime()).activateWorkflowTool(); }); pi.registerCommand("ultra", { description: "Enable ultra, execute workflow instructions, or run a saved workflow.", getArgumentCompletions: async (prefix) => (await runtime()).getArgumentCompletions(prefix), handler: async (argString, ctx) => (await runtime()).handleCommand(argString, ctx), }); }