Merge pull request #513 from austinm911/fix/async-extension-factories

feat(extensions): support async extension factory functions
This commit is contained in:
Mario Zechner 2026-01-07 11:48:33 +01:00 committed by GitHub
commit 10e651f99b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 8 deletions

View file

@ -313,7 +313,7 @@ async function loadExtensionWithBun(
setFlagValue, setFlagValue,
} = createExtensionAPI(handlers, tools, cwd, extensionPath, eventBus, sharedUI); } = createExtensionAPI(handlers, tools, cwd, extensionPath, eventBus, sharedUI);
factory(api); await factory(api);
return { return {
extension: { extension: {
@ -401,7 +401,7 @@ async function loadExtension(
setFlagValue, setFlagValue,
} = createExtensionAPI(handlers, tools, cwd, extensionPath, eventBus, sharedUI); } = createExtensionAPI(handlers, tools, cwd, extensionPath, eventBus, sharedUI);
factory(api); await factory(api);
return { return {
extension: { extension: {
@ -436,13 +436,13 @@ async function loadExtension(
/** /**
* Create a LoadedExtension from an inline factory function. * Create a LoadedExtension from an inline factory function.
*/ */
export function loadExtensionFromFactory( export async function loadExtensionFromFactory(
factory: ExtensionFactory, factory: ExtensionFactory,
cwd: string, cwd: string,
eventBus: EventBus, eventBus: EventBus,
sharedUI: { ui: ExtensionUIContext; hasUI: boolean }, sharedUI: { ui: ExtensionUIContext; hasUI: boolean },
name = "<inline>", name = "<inline>",
): LoadedExtension { ): Promise<LoadedExtension> {
const handlers = new Map<string, HandlerFn[]>(); const handlers = new Map<string, HandlerFn[]>();
const tools = new Map<string, RegisteredTool>(); const tools = new Map<string, RegisteredTool>();
const { const {
@ -464,7 +464,7 @@ export function loadExtensionFromFactory(
setFlagValue, setFlagValue,
} = createExtensionAPI(handlers, tools, cwd, name, eventBus, sharedUI); } = createExtensionAPI(handlers, tools, cwd, name, eventBus, sharedUI);
factory(api); await factory(api);
return { return {
path: name, path: name,

View file

@ -649,8 +649,8 @@ export interface ExtensionAPI {
events: EventBus; events: EventBus;
} }
/** Extension factory function type. */ /** Extension factory function type. Supports both sync and async initialization. */
export type ExtensionFactory = (pi: ExtensionAPI) => void; export type ExtensionFactory = (pi: ExtensionAPI) => void | Promise<void>;
// ============================================================================ // ============================================================================
// Loaded Extension Types // Loaded Extension Types

View file

@ -488,7 +488,7 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
}; };
for (let i = 0; i < options.extensions.length; i++) { for (let i = 0; i < options.extensions.length; i++) {
const factory = options.extensions[i]; const factory = options.extensions[i];
const loaded = loadExtensionFromFactory(factory, cwd, eventBus, uiHolder, `<inline-${i}>`); const loaded = await loadExtensionFromFactory(factory, cwd, eventBus, uiHolder, `<inline-${i}>`);
extensionsResult.extensions.push(loaded); extensionsResult.extensions.push(loaded);
} }
// Extend setUIContext to update inline extensions too // Extend setUIContext to update inline extensions too