MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / runServerInProcess

Function runServerInProcess

apps/kimi-code/src/cli/sub/server/run.ts:288–368  ·  view source on GitHub ↗

* Start the server in the current process and block until shutdown. Shared by * the detached daemon (`daemon: true`, with idle-exit) and the foreground * runner (`daemon: false`). `onReady` fires once the server is listening.

(
  options: ParsedServerOptions,
  mode: { daemon: boolean },
  onReady?: (origin: string) => void,
)

Source from the content-addressed store, hash-verified

286 * runner (`daemon: false`). `onReady` fires once the server is listening.
287 */
288async function runServerInProcess(
289 options: ParsedServerOptions,
290 mode: { daemon: boolean },
291 onReady?: (origin: string) => void,
292): Promise<never> {
293 const version = getVersion();
294 const telemetry = initializeServerTelemetry({ version });
295
296 let running: RunningServer | undefined;
297 let stopping = false;
298
299 const idle = mode.daemon
300 ? createIdleShutdownHandler({
301 graceMs: options.idleGraceMs,
302 onIdle: () => {
303 void shutdown('idle');
304 },
305 })
306 : undefined;
307
308 async function shutdown(reason: string): Promise<void> {
309 if (stopping) return;
310 stopping = true;
311 idle?.cancel();
312 running?.logger.info({ reason }, 'server shutting down');
313 try {
314 await running?.close();
315 await shutdownTelemetry({ timeoutMs: CLI_SHUTDOWN_TIMEOUT_MS });
316 } catch (error) {
317 running?.logger.error(
318 { err: error instanceof Error ? error : new Error(String(error)) },
319 'server shutdown error',
320 );
321 }
322 process.exit(0);
323 }
324
325 running = await startServer({
326 host: options.host,
327 port: options.port,
328 logLevel: options.logLevel,
329 debugEndpoints: options.debugEndpoints,
330 insecureNoTls: options.insecureNoTls,
331 allowRemoteShutdown: options.allowRemoteShutdown,
332 allowRemoteTerminals: options.allowRemoteTerminals,
333 allowedHosts: options.allowedHosts,
334 webAssetsDir: serverWebAssetsDir(),
335 coreProcessOptions: {
336 identity: createKimiCodeHostIdentity(version),
337 telemetry,
338 },
339 wsGatewayOptions: {
340 telemetry,
341 onConnectionCountChange: idle
342 ? (size) => {
343 idle.onConnectionCountChange(size);
344 }
345 : undefined,

Callers 2

startServerDaemonFunction · 0.85
startServerForegroundFunction · 0.85

Calls 10

getVersionFunction · 0.90
startServerFunction · 0.90
serverWebAssetsDirFunction · 0.85
shutdownFunction · 0.70
onceMethod · 0.65
infoMethod · 0.65
trackFunction · 0.50

Tested by

no test coverage detected