From f0063e154960dcfed54d07e5ab5f282e7a947a75 Mon Sep 17 00:00:00 2001 From: leduclinh Date: Wed, 4 Feb 2026 18:40:17 +0900 Subject: [PATCH] feat: update version to 2.2.2 and enhance server startup logic to handle port conflicts --- package-lock.json | 4 ++-- package.json | 2 +- src/cli/web-viewer.ts | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 17af2a6..15d3fdb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@aitytech/agentkits-memory", - "version": "2.2.1", + "version": "2.2.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@aitytech/agentkits-memory", - "version": "2.2.1", + "version": "2.2.2", "license": "MIT", "dependencies": { "@anthropic-ai/claude-agent-sdk": "^0.2.29", diff --git a/package.json b/package.json index ba072b1..2a42081 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@aitytech/agentkits-memory", - "version": "2.2.1", + "version": "2.2.2", "type": "module", "description": "Persistent memory system for AI coding assistants via MCP. Works with Claude Code, Cursor, Copilot, Windsurf, Cline.", "main": "dist/index.js", diff --git a/src/cli/web-viewer.ts b/src/cli/web-viewer.ts index 70b7a6c..0d58482 100644 --- a/src/cli/web-viewer.ts +++ b/src/cli/web-viewer.ts @@ -2923,12 +2923,37 @@ function handleRequest( const server = http.createServer(handleRequest); -server.listen(PORT, () => { - console.log(`\n AgentKits Memory Viewer\n`); - console.log(` Local: http://localhost:${PORT}`); - console.log(` Database: ${dbPath}\n`); - console.log(` Press Ctrl+C to stop\n`); -}); +/** Try to listen on the given port; on EADDRINUSE, pick a random available port. */ +function startServer(port: number) { + server.listen(port, () => { + const addr = server.address(); + const actualPort = typeof addr === 'object' && addr ? addr.port : port; + console.log(`\n AgentKits Memory Viewer\n`); + console.log(` Local: http://localhost:${actualPort}`); + console.log(` Database: ${dbPath}\n`); + console.log(` Press Ctrl+C to stop\n`); + }); + + server.on('error', (err: NodeJS.ErrnoException) => { + if (err.code === 'EADDRINUSE') { + console.log(` Port ${port} is in use, finding an available port...`); + // Listen on port 0 to let the OS assign a random available port + server.listen(0, () => { + const addr = server.address(); + const actualPort = typeof addr === 'object' && addr ? addr.port : 0; + console.log(`\n AgentKits Memory Viewer\n`); + console.log(` Local: http://localhost:${actualPort}`); + console.log(` Database: ${dbPath}\n`); + console.log(` Press Ctrl+C to stop\n`); + }); + } else { + console.error(` Failed to start server: ${err.message}`); + process.exit(1); + } + }); +} + +startServer(PORT); // Graceful shutdown: close server, DB, and embeddings service on SIGINT/SIGTERM function cleanup() {