| 97 | |
| 98 | // Helper to report unhandled errors to the API when running in serve mode |
| 99 | async function reportUnhandledErrorToApi(error: Error): Promise<void> { |
| 100 | if (!agentId) { |
| 101 | // Not running in serve mode with an agent ID, skip API reporting |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | try { |
| 106 | await post(`agents/${agentId}/status`, { |
| 107 | status: "FAILED", |
| 108 | errorMessage: `Unhandled error: ${error.message}`, |
| 109 | }); |
| 110 | logger.debug(`Reported unhandled error to API for agent ${agentId}`); |
| 111 | } catch (apiError) { |
| 112 | // If API reporting fails, just log it - don't crash |
| 113 | logger.debug( |
| 114 | `Failed to report error to API: ${apiError instanceof Error ? apiError.message : String(apiError)}`, |
| 115 | ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Add global error handlers to prevent uncaught errors from crashing the process |
| 120 | process.on("unhandledRejection", (reason, promise) => { |