5e9ef021bb
- Added thinkingLevel field to agent frontmatter allowing subagents to override thinking level with clamping to model capabilities. - Added structured output schema to reviewer agent ensuring consistent review output format with findings, correctness verdict, and confidence. - Expanded system prompt with defensive reasoning guidance including assumption checks, edge case awareness, and completion reflex inhibition. - Consolidated expandPath, parseFrontmatter, and parseAgentFields utilities into discovery/helpers module eliminating code duplication across loaders. - Replaced silent error handling with logger.warn calls across config parsing, migrations, extension loading, and OAuth refresh failures.
272 lines
7.9 KiB
TypeScript
272 lines
7.9 KiB
TypeScript
/**
|
|
* Hook loader - loads TypeScript hook modules using native Bun import.
|
|
*/
|
|
|
|
import * as path from "node:path";
|
|
import * as typebox from "@sinclair/typebox";
|
|
import { hookCapability } from "../../capability/hook";
|
|
import type { Hook } from "../../discovery";
|
|
import { loadCapability } from "../../discovery";
|
|
import { expandPath } from "../../discovery/helpers";
|
|
import * as piCodingAgent from "../../index";
|
|
import { logger } from "../logger";
|
|
import type { HookMessage } from "../messages";
|
|
import type { SessionManager } from "../session-manager";
|
|
import { execCommand } from "./runner";
|
|
import type { ExecOptions, HookAPI, HookFactory, HookMessageRenderer, RegisteredCommand } from "./types";
|
|
|
|
/**
|
|
* Generic handler function type.
|
|
*/
|
|
type HandlerFn = (...args: unknown[]) => Promise<unknown>;
|
|
|
|
/**
|
|
* Send message handler type for pi.sendMessage().
|
|
*/
|
|
export type SendMessageHandler = <T = unknown>(
|
|
message: Pick<HookMessage<T>, "customType" | "content" | "display" | "details">,
|
|
triggerTurn?: boolean,
|
|
) => void;
|
|
|
|
/**
|
|
* Append entry handler type for pi.appendEntry().
|
|
*/
|
|
export type AppendEntryHandler = <T = unknown>(customType: string, data?: T) => void;
|
|
|
|
/**
|
|
* New session handler type for ctx.newSession() in HookCommandContext.
|
|
*/
|
|
export type NewSessionHandler = (options?: {
|
|
parentSession?: string;
|
|
setup?: (sessionManager: SessionManager) => Promise<void>;
|
|
}) => Promise<{ cancelled: boolean }>;
|
|
|
|
/**
|
|
* Branch handler type for ctx.branch() in HookCommandContext.
|
|
*/
|
|
export type BranchHandler = (entryId: string) => Promise<{ cancelled: boolean }>;
|
|
|
|
/**
|
|
* Navigate tree handler type for ctx.navigateTree() in HookCommandContext.
|
|
*/
|
|
export type NavigateTreeHandler = (
|
|
targetId: string,
|
|
options?: { summarize?: boolean },
|
|
) => Promise<{ cancelled: boolean }>;
|
|
|
|
/**
|
|
* Registered handlers for a loaded hook.
|
|
*/
|
|
export interface LoadedHook {
|
|
/** Original path from config */
|
|
path: string;
|
|
/** Resolved absolute path */
|
|
resolvedPath: string;
|
|
/** Map of event type to handler functions */
|
|
handlers: Map<string, HandlerFn[]>;
|
|
/** Map of customType to hook message renderer */
|
|
messageRenderers: Map<string, HookMessageRenderer>;
|
|
/** Map of command name to registered command */
|
|
commands: Map<string, RegisteredCommand>;
|
|
/** Set the send message handler for this hook's pi.sendMessage() */
|
|
setSendMessageHandler: (handler: SendMessageHandler) => void;
|
|
/** Set the append entry handler for this hook's pi.appendEntry() */
|
|
setAppendEntryHandler: (handler: AppendEntryHandler) => void;
|
|
}
|
|
|
|
/**
|
|
* Result of loading hooks.
|
|
*/
|
|
export interface LoadHooksResult {
|
|
/** Successfully loaded hooks */
|
|
hooks: LoadedHook[];
|
|
/** Errors encountered during loading */
|
|
errors: Array<{ path: string; error: string }>;
|
|
}
|
|
|
|
/**
|
|
* Resolve hook path.
|
|
* - Absolute paths used as-is
|
|
* - Paths starting with ~ expanded to home directory
|
|
* - Relative paths resolved from cwd
|
|
*/
|
|
function resolveHookPath(hookPath: string, cwd: string): string {
|
|
const expanded = expandPath(hookPath);
|
|
|
|
if (path.isAbsolute(expanded)) {
|
|
return expanded;
|
|
}
|
|
|
|
// Relative paths resolved from cwd
|
|
return path.resolve(cwd, expanded);
|
|
}
|
|
|
|
/**
|
|
* Create a HookAPI instance that collects handlers, renderers, and commands.
|
|
* Returns the API, maps, and functions to set handlers later.
|
|
*/
|
|
function createHookAPI(
|
|
handlers: Map<string, HandlerFn[]>,
|
|
cwd: string,
|
|
): {
|
|
api: HookAPI;
|
|
messageRenderers: Map<string, HookMessageRenderer>;
|
|
commands: Map<string, RegisteredCommand>;
|
|
setSendMessageHandler: (handler: SendMessageHandler) => void;
|
|
setAppendEntryHandler: (handler: AppendEntryHandler) => void;
|
|
} {
|
|
let sendMessageHandler: SendMessageHandler | null = null;
|
|
let appendEntryHandler: AppendEntryHandler | null = null;
|
|
const messageRenderers = new Map<string, HookMessageRenderer>();
|
|
const commands = new Map<string, RegisteredCommand>();
|
|
|
|
// Cast to HookAPI - the implementation is more general (string event names)
|
|
// but the interface has specific overloads for type safety in hooks
|
|
const api = {
|
|
on(event: string, handler: HandlerFn): void {
|
|
if (!handlers.has(event)) {
|
|
handlers.set(event, []);
|
|
}
|
|
handlers.get(event)!.push(handler);
|
|
},
|
|
sendMessage<T = unknown>(message: HookMessage<T>, triggerTurn?: boolean): void {
|
|
if (!sendMessageHandler) {
|
|
throw new Error("sendMessage handler not initialized");
|
|
}
|
|
sendMessageHandler(message, triggerTurn);
|
|
},
|
|
appendEntry<T = unknown>(customType: string, data?: T): void {
|
|
if (!appendEntryHandler) {
|
|
throw new Error("appendEntry handler not initialized");
|
|
}
|
|
appendEntryHandler(customType, data);
|
|
},
|
|
registerMessageRenderer<T = unknown>(customType: string, renderer: HookMessageRenderer<T>): void {
|
|
messageRenderers.set(customType, renderer as HookMessageRenderer);
|
|
},
|
|
registerCommand(name: string, options: { description?: string; handler: RegisteredCommand["handler"] }): void {
|
|
commands.set(name, { name, ...options });
|
|
},
|
|
exec(command: string, args: string[], options?: ExecOptions) {
|
|
return execCommand(command, args, options?.cwd ?? cwd, options);
|
|
},
|
|
logger,
|
|
typebox,
|
|
pi: piCodingAgent,
|
|
} as HookAPI;
|
|
|
|
return {
|
|
api,
|
|
messageRenderers,
|
|
commands,
|
|
setSendMessageHandler: (handler: SendMessageHandler) => {
|
|
sendMessageHandler = handler;
|
|
},
|
|
setAppendEntryHandler: (handler: AppendEntryHandler) => {
|
|
appendEntryHandler = handler;
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Load a single hook module using native Bun import.
|
|
*/
|
|
async function loadHook(hookPath: string, cwd: string): Promise<{ hook: LoadedHook | null; error: string | null }> {
|
|
const resolvedPath = resolveHookPath(hookPath, cwd);
|
|
|
|
try {
|
|
// Import the module using native Bun import
|
|
const module = await import(resolvedPath);
|
|
const factory = module.default as HookFactory;
|
|
|
|
if (typeof factory !== "function") {
|
|
return { hook: null, error: "Hook must export a default function" };
|
|
}
|
|
|
|
// Create handlers map and API
|
|
const handlers = new Map<string, HandlerFn[]>();
|
|
const { api, messageRenderers, commands, setSendMessageHandler, setAppendEntryHandler } = createHookAPI(
|
|
handlers,
|
|
cwd,
|
|
);
|
|
|
|
// Call factory to register handlers
|
|
factory(api);
|
|
|
|
return {
|
|
hook: {
|
|
path: hookPath,
|
|
resolvedPath,
|
|
handlers,
|
|
messageRenderers,
|
|
commands,
|
|
setSendMessageHandler,
|
|
setAppendEntryHandler,
|
|
},
|
|
error: null,
|
|
};
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
return { hook: null, error: `Failed to load hook: ${message}` };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load all hooks from configuration.
|
|
* @param paths - Array of hook file paths
|
|
* @param cwd - Current working directory for resolving relative paths
|
|
*/
|
|
export async function loadHooks(paths: string[], cwd: string): Promise<LoadHooksResult> {
|
|
const hooks: LoadedHook[] = [];
|
|
const errors: Array<{ path: string; error: string }> = [];
|
|
|
|
for (const hookPath of paths) {
|
|
const { hook, error } = await loadHook(hookPath, cwd);
|
|
|
|
if (error) {
|
|
errors.push({ path: hookPath, error });
|
|
continue;
|
|
}
|
|
|
|
if (hook) {
|
|
hooks.push(hook);
|
|
}
|
|
}
|
|
|
|
return { hooks, errors };
|
|
}
|
|
|
|
/**
|
|
* Discover and load hooks from all registered providers.
|
|
* Uses the capability API to discover hook paths from:
|
|
* 1. OMP native configs (.omp/.pi hooks/)
|
|
* 2. Installed plugins
|
|
* 3. Other editor/IDE configurations
|
|
*
|
|
* Plus any explicitly configured paths from settings.
|
|
*/
|
|
export async function discoverAndLoadHooks(configuredPaths: string[], cwd: string): Promise<LoadHooksResult> {
|
|
const allPaths: string[] = [];
|
|
const seen = new Set<string>();
|
|
|
|
// Helper to add paths without duplicates
|
|
const addPaths = (paths: string[]) => {
|
|
for (const p of paths) {
|
|
const resolved = path.resolve(p);
|
|
if (!seen.has(resolved)) {
|
|
seen.add(resolved);
|
|
allPaths.push(p);
|
|
}
|
|
}
|
|
};
|
|
|
|
// 1. Discover hooks via capability API
|
|
const discovered = await loadCapability<Hook>(hookCapability.id, { cwd });
|
|
addPaths(discovered.items.map((hook) => hook.path));
|
|
|
|
// 2. Explicitly configured paths (can override/add)
|
|
addPaths(configuredPaths.map((p) => resolveHookPath(p, cwd)));
|
|
|
|
return loadHooks(allPaths, cwd);
|
|
}
|