#!/usr/bin/env node
/**
* Debug Script: Ask-One-Question Tool
*
* Simulates ask-one-question MCP tool behavior for UI development.
* Starts a mock server, sends a realistic question, waits for response,
* then outputs the formatted result that would be sent to the LLM.
*/
import { MockServer } from './shared/mock-server.js';
import { ResponseSimulator } from './shared/response-simulator.js';
import { mockAskOneQuestionData, mockResponses } from './shared/mock-data.js';
async function runDebugAskOneQuestion(): Promise<void> {
const server = new MockServer(3000);
const simulator = new ResponseSimulator(server, {
responseDelay: 2500, // 2.5 seconds
simulateTyping: false
});
try {
// Start the mock server
await server.start();
console.error('[Debug] Ask-One-Question debug server started');
console.error('[Debug] Open http://localhost:3000 in your browser to see the UI');
console.error('[Debug] Waiting for UI connection...');
// Wait a moment for potential UI connection
await new Promise(resolve => setTimeout(resolve, 2000));
// Create the mock request
const requestId = `ask-one-question-${Date.now()}`;
const mockRequest = {
id: requestId,
type: 'ask-one-question',
data: {
...mockAskOneQuestionData,
timestamp: new Date().toISOString(),
context: {
...mockAskOneQuestionData.context,
debugMode: true
}
},
timestamp: new Date().toISOString()
};
// Add the request (this triggers SSE to UI)
server.addRequest(mockRequest);
console.error(`[Debug] Sent ask-one-question request: ${requestId}`);
// Set up response handler
const responsePromise = new Promise<any>((resolve) => {
server.once('response', (data) => {
resolve(data.response);
});
});
// Auto-simulate response after delay if no UI response
const autoResponsePromise = new Promise<any>(async (resolve) => {
await new Promise(r => setTimeout(r, 8000)); // Wait 8 seconds
if (server.hasPendingRequests()) {
console.error('[Debug] No UI response received, using mock response');
const mockResponse = await simulator.simulateResponse(mockRequest);
resolve(mockResponse);
}
});
// Wait for either UI response or auto-response
const humanResponse = await Promise.race([responsePromise, autoResponsePromise]);
// Format the final result as it would be sent to LLM
const llmResult = formatAskOneQuestionResult(mockRequest, humanResponse);
// Output the result to stdout (this is what the LLM would receive)
console.log('='.repeat(80));
console.log('FORMATTED RESULT FOR LLM:');
console.log('='.repeat(80));
console.log(llmResult);
console.log('='.repeat(80));
} catch (error) {
console.error('[Debug] Error:', error);
process.exit(1);
} finally {
await server.stop();
console.error('[Debug] Ask-One-Question debug session completed');
process.exit(0);
}
}
/**
* Format the ask-one-question result as it would be returned to the LLM
*/
function formatAskOneQuestionResult(request: any, response: any): string {
const question = request.data.question;
const answer = response.answer || 'No response provided';
const timestamp = response.timestamp || new Date().toISOString();
const context = request.data.context || {};
return `# Human Response to Question
## Original Question
${question}
## Context
${context.project ? `**Project:** ${context.project}` : ''}
${context.team ? `**Team:** ${context.team}` : ''}
${context.deadline ? `**Deadline:** ${context.deadline}` : ''}
${context.budget ? `**Budget:** ${context.budget}` : ''}
## Human Answer
${answer}
## Response Details
- **Timestamp:** ${timestamp}
- **Response Type:** Free-form text answer
- **Session:** demo
---
**Note:** The human has provided their input. Use this response to inform your next steps or recommendations.`;
}
// CLI execution
if (require.main === module) {
console.error('[Debug] Starting Ask-One-Question debug session...');
runDebugAskOneQuestion().catch(error => {
console.error('[Debug] Fatal error:', error);
process.exit(1);
});
}
export { runDebugAskOneQuestion, formatAskOneQuestionResult };