Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/agent-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,9 @@ export async function loadAgent(agentPath: string): Promise<FluxAgent> {

const moduleUrl = pathToFileURL(agentPath).href;
const agentModule = await import(moduleUrl);
return agentModule.default as FluxAgent;
const agent = agentModule.default as FluxAgent;
if (agent.onInit) {
await agent.onInit();
Copy link

Copilot AI Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onInit hook should be wrapped in a try-catch block to handle potential initialization errors gracefully. If onInit throws an error, it will currently cause an unhandled promise rejection and the agent will fail to load without clear feedback to the user about what went wrong during initialization.

Suggested change
await agent.onInit();
try {
await agent.onInit();
} catch (error) {
console.error("[FLUX] Agent onInit hook failed:", error);
throw error;
}

Copilot uses AI. Check for mistakes.
}
return agent;
}
6 changes: 6 additions & 0 deletions src/agent-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// Defines the FluxAgent interface that user agents must implement.
// onInit is called once when the agent is loaded.
// onShutdown is called on graceful process exit.

export interface FluxAgent {
onInit?: () => Promise<void>;
invoke: (input: { message: string; userPhoneNumber: string; imageBase64?: string }) => Promise<string>;
onError?: (error: Error) => Promise<void>;
Copy link

Copilot AI Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation comment for onError lifecycle hook is missing. While onInit and onShutdown have comments explaining when they are called, onError lacks this documentation. For consistency and clarity, add a comment explaining when onError is invoked, such as "onError is called when invoke throws an error."

Copilot uses AI. Check for mistakes.
onShutdown?: () => Promise<void>;
}
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ async function runLocal() {
console.log(`Agent: ${response}\n`);
} catch (error: any) {
console.log(`[FLUX] Error: ${error.message}\n`);
if (agent.onError) {
await agent.onError(error);
Copy link

Copilot AI Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onError hook should be wrapped in a try-catch block to prevent errors within the error handler from causing unhandled promise rejections. If onError itself throws an error, it could lead to cascading failures and unclear error messages for users.

Suggested change
await agent.onError(error);
try {
await agent.onError(error);
} catch (handlerError) {
console.error("[FLUX] Error in agent.onError handler:", handlerError);
}

Copilot uses AI. Check for mistakes.
}
}

askQuestion();
});
};

rl.on("close", () => {
rl.on("close", async () => {
console.log("\n[FLUX] Goodbye!");
if (agent.onShutdown) {
await agent.onShutdown();
Copy link

Copilot AI Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onShutdown hook should be wrapped in a try-catch block to ensure the process can exit gracefully even if the shutdown hook throws an error. Without this protection, errors in onShutdown could prevent the application from exiting properly or cause unclear error messages during shutdown.

Suggested change
await agent.onShutdown();
try {
await agent.onShutdown();
} catch (error: any) {
console.error(`[FLUX] Error during shutdown: ${error?.message ?? error}`);
}

Copilot uses AI. Check for mistakes.
}
process.exit(0);
});

Expand Down Expand Up @@ -130,6 +136,9 @@ async function runProd() {

process.on("SIGINT", async () => {
console.log("\n[FLUX] Shutting down...");
if (agent.onShutdown) {
await agent.onShutdown();
}
await flux.disconnect();
process.exit(0);
Comment on lines +139 to 143
Copy link

Copilot AI Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onShutdown hook should be wrapped in a try-catch block to ensure the process can exit gracefully even if the shutdown hook throws an error. Without this protection, errors in onShutdown could prevent flux.disconnect() from being called or cause unclear error messages during shutdown.

Suggested change
if (agent.onShutdown) {
await agent.onShutdown();
}
await flux.disconnect();
process.exit(0);
try {
if (agent.onShutdown) {
await agent.onShutdown();
}
} catch (error: any) {
console.error("[FLUX] Error during agent shutdown:", error?.message ?? error);
} finally {
await flux.disconnect();
process.exit(0);
}

Copilot uses AI. Check for mistakes.
});
Expand Down