| 70 | |
| 71 | // 创建一个Promise来处理服务器响应 |
| 72 | function queryServer(command) { |
| 73 | return new Promise((resolve, reject) => { |
| 74 | console.log('\n🔄 Starting debug server process...'); |
| 75 | |
| 76 | const serverProcess = spawn('node', ['debug-local.js'], { |
| 77 | cwd: __dirname, |
| 78 | stdio: ['pipe', 'pipe', 'inherit'] |
| 79 | }); |
| 80 | |
| 81 | let responseData = ''; |
| 82 | let serverReady = false; |
| 83 | |
| 84 | // 监听服务器输出 |
| 85 | serverProcess.stdout.on('data', (data) => { |
| 86 | const output = data.toString(); |
| 87 | console.log('📝 Server output:', output); |
| 88 | |
| 89 | // 检查服务器是否已经准备好 |
| 90 | if (output.includes('Search index loaded with') && !serverReady) { |
| 91 | serverReady = true; |
| 92 | console.log('\n🚀 Server is ready, sending query...'); |
| 93 | |
| 94 | // 发送命令 |
| 95 | console.log('\n📤 Sending query:', JSON.stringify(command, null, 2)); |
| 96 | serverProcess.stdin.write(JSON.stringify(command) + '\n'); |
| 97 | } |
| 98 | |
| 99 | // 尝试解析响应 |
| 100 | try { |
| 101 | if (output.includes('"result"') || output.includes('"error"')) { |
| 102 | const jsonStart = output.indexOf('{'); |
| 103 | const jsonEnd = output.lastIndexOf('}') + 1; |
| 104 | if (jsonStart !== -1 && jsonEnd !== -1) { |
| 105 | const jsonStr = output.substring(jsonStart, jsonEnd); |
| 106 | const response = JSON.parse(jsonStr); |
| 107 | |
| 108 | console.log('\n✅ Received response:', JSON.stringify(response, null, 2)); |
| 109 | |
| 110 | // 关闭服务器 |
| 111 | serverProcess.kill(); |
| 112 | |
| 113 | if (response.error) { |
| 114 | reject(new Error(response.error.message)); |
| 115 | } else { |
| 116 | resolve(response); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } catch (e) { |
| 121 | console.log('⚠️ Parse error:', e.message); |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | // 错误处理 |
| 126 | serverProcess.on('error', (error) => { |
| 127 | console.error('❌ Server process error:', error); |
| 128 | reject(error); |
| 129 | }); |