-
Notifications
You must be signed in to change notification settings - Fork 10
Wire FluxAgent lifecycle hooks (init, error, shutdown) #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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>; | ||
|
||
| onShutdown?: () => Promise<void>; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| await agent.onError(error); | |
| try { | |
| await agent.onError(error); | |
| } catch (handlerError) { | |
| console.error("[FLUX] Error in agent.onError handler:", handlerError); | |
| } |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
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.
| await agent.onShutdown(); | |
| try { | |
| await agent.onShutdown(); | |
| } catch (error: any) { | |
| console.error(`[FLUX] Error during shutdown: ${error?.message ?? error}`); | |
| } |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
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.
| 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); | |
| } |
There was a problem hiding this comment.
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.