import { getOrderArgumentCompletions, handleOrderCommand, type OrderAutocompleteItem, type OrderCommandContext, } from "./commands/order.js"; interface AutocompleteSuggestions { items: OrderAutocompleteItem[]; prefix: string; } interface AutocompleteProvider { getSuggestions( lines: string[], cursorLine: number, cursorCol: number, options: { signal: AbortSignal; force?: boolean }, ): Promise; applyCompletion( lines: string[], cursorLine: number, cursorCol: number, item: OrderAutocompleteItem, prefix: string, ): { lines: string[]; cursorLine: number; cursorCol: number }; shouldTriggerFileCompletion?(lines: string[], cursorLine: number, cursorCol: number): boolean; } interface RegisteredCommand { description?: string; argumentHint?: string; getArgumentCompletions?: (argumentPrefix: string) => Promise; handler: (args: string, ctx: OrderCommandContext) => Promise; } interface ExtensionContext { ui?: { addAutocompleteProvider?(factory: (current: AutocompleteProvider) => AutocompleteProvider): void; }; } interface ExtensionAPI { registerCommand(name: string, options: RegisteredCommand): void; on?(event: "session_start", handler: (_event: unknown, ctx: ExtensionContext) => void): void; } export default function registerOrderExtension(pi: ExtensionAPI): void { pi.registerCommand("order", { description: "Manage local prompt templates", argumentHint: "create|edit|send|remove|list", getArgumentCompletions: getOrderArgumentCompletions, handler: async (args, ctx) => { await handleOrderCommand(args, ctx); }, }); pi.on?.("session_start", (_event, ctx) => { ctx.ui?.addAutocompleteProvider?.((current) => ({ async getSuggestions(lines, cursorLine, cursorCol, options) { const line = lines[cursorLine] ?? ""; const beforeCursor = line.slice(0, cursorCol); const match = beforeCursor.match(/^\/order(?:\s+([\s\S]*))?$/); if (!match) { return current.getSuggestions(lines, cursorLine, cursorCol, options); } const argumentPrefix = match[1] ?? ""; const suggestions = await getOrderArgumentCompletions(argumentPrefix); if (!suggestions || suggestions.length === 0) { return current.getSuggestions(lines, cursorLine, cursorCol, options); } return { items: suggestions, prefix: beforeCursor, }; }, applyCompletion(lines, cursorLine, cursorCol, item, prefix) { const line = lines[cursorLine] ?? ""; const beforeCursor = line.slice(0, cursorCol); if (prefix === beforeCursor && beforeCursor.startsWith("/order")) { const completedText = `/order ${item.value}`; const nextLine = `${line.slice(0, cursorCol - prefix.length)}${completedText}${line.slice(cursorCol)}`; return { lines: lines.map((existingLine, index) => (index === cursorLine ? nextLine : existingLine)), cursorLine, cursorCol: completedText.length, }; } return current.applyCompletion(lines, cursorLine, cursorCol, item, prefix); }, shouldTriggerFileCompletion(lines, cursorLine, cursorCol) { return current.shouldTriggerFileCompletion?.(lines, cursorLine, cursorCol) ?? true; }, })); }); } export { getOrderArgumentCompletions, handleOrderCommand };