()
| 133 | }; |
| 134 | |
| 135 | export function main(): void { |
| 136 | process.title = PROCESS_NAME; |
| 137 | installCrashHandlers(); |
| 138 | // Route all outbound fetch through HTTP_PROXY/HTTPS_PROXY (honoring NO_PROXY) |
| 139 | // before any client is constructed. No-op when no proxy variable is set; an |
| 140 | // invalid proxy URL is reported and ignored rather than aborting startup. |
| 141 | installGlobalProxyDispatcher(); |
| 142 | installNativeModuleHook(); |
| 143 | if (runNativeAssetSmokeIfRequested()) return; |
| 144 | |
| 145 | // Start the background cleanup of stale native cache. Fire-and-forget; must not block startup or throw. |
| 146 | queueMicrotask(() => { |
| 147 | try { |
| 148 | cleanupStaleNativeCacheForCurrent(); |
| 149 | } catch { |
| 150 | // ignore: cache GC must never affect process startup |
| 151 | } |
| 152 | }); |
| 153 | |
| 154 | const version = getVersion(); |
| 155 | |
| 156 | const program = createProgram( |
| 157 | version, |
| 158 | (opts) => { |
| 159 | void handleMainCommand(opts, version) |
| 160 | .then(async (outcome) => { |
| 161 | // Only the process entrypoint disposes of the process. Print mode |
| 162 | // relies on the event loop draining to exit; flush any buffered output |
| 163 | // and then arm an unref'd fallback so a stray ref'd handle left over |
| 164 | // from the run can't wedge a completed `kimi -p` until an external |
| 165 | // timeout. A healthy run drains and exits before the fallback fires. |
| 166 | if (outcome.headlessCompleted) { |
| 167 | await finalizeHeadlessRun( |
| 168 | process, |
| 169 | [process.stdout, process.stderr], |
| 170 | () => Number(process.exitCode) || 0, |
| 171 | ); |
| 172 | } |
| 173 | }) |
| 174 | .catch(async (error: unknown) => { |
| 175 | const operation = opts.prompt !== undefined ? 'run prompt' : 'start shell'; |
| 176 | await logStartupFailure(operation, error); |
| 177 | process.stderr.write( |
| 178 | formatStartupError(error, { |
| 179 | operation, |
| 180 | }), |
| 181 | ); |
| 182 | process.stderr.write(`See log: ${resolveGlobalLogPath(resolveKimiHome())}\n`); |
| 183 | process.exit(1); |
| 184 | }); |
| 185 | }, |
| 186 | () => { |
| 187 | void handleMigrateCommand(version).catch(async (error: unknown) => { |
| 188 | await logStartupFailure('run migration', error); |
| 189 | process.stderr.write(formatStartupError(error, { operation: 'run migration' })); |
| 190 | process.stderr.write(`See log: ${resolveGlobalLogPath(resolveKimiHome())}\n`); |
| 191 | process.exit(1); |
| 192 | }); |
no test coverage detected