mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 06:04:15 +00:00
feat(extensions): support async extension factory functions
Extensions can now use async initialization, enabling: - Dynamic imports (e.g., loading tools from external packages) - Async setup (config fetching, service connections) - Lazy-loaded dependencies Changes: - ExtensionFactory type now returns void | Promise<void> - loadExtensionFromFactory is now async, returns Promise<LoadedExtension> - All factory(api) calls are now awaited Backwards compatible: sync extensions continue to work unchanged.
This commit is contained in:
parent
282273e156
commit
aea9f8439d
3 changed files with 8 additions and 8 deletions
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -641,8 +641,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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue