Update server.js
Browse files
server.js
CHANGED
|
@@ -41,6 +41,48 @@ app.get("/health", (req, res) => {
|
|
| 41 |
});
|
| 42 |
});
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
// Helper function to run OpenClaw agent
|
| 45 |
function runOpenClawAgent(env, payload) {
|
| 46 |
return new Promise((resolve, reject) => {
|
|
@@ -124,6 +166,12 @@ app.post("/api/market-research", async (req, res) => {
|
|
| 124 |
console.log('Calling OpenClaw agent...');
|
| 125 |
const agentResult = await runOpenClawAgent(env, { keyword });
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
// Transform agent result to n8n format
|
| 128 |
const response = transformToN8nFormat(agentResult, keyword);
|
| 129 |
console.log('Returning transformed response');
|
|
@@ -131,6 +179,14 @@ app.post("/api/market-research", async (req, res) => {
|
|
| 131 |
|
| 132 |
} catch (error) {
|
| 133 |
console.error('OpenClaw agent error:', error.message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
// Return fallback data if agent fails
|
| 135 |
const fallbackResponse = createFallbackResponse(keyword);
|
| 136 |
console.log('Returning fallback response');
|
|
@@ -374,21 +430,6 @@ function createFallbackResponse(keyword) {
|
|
| 374 |
// Also include job_id at root for n8n compatibility
|
| 375 |
job_id: fallback_job_id
|
| 376 |
};
|
| 377 |
-
|
| 378 |
-
const job_id = `job_${Date.now()}`;
|
| 379 |
-
const transform_job_id = `job_${Date.now()}`;
|
| 380 |
-
return {
|
| 381 |
-
dashboard_view: dashboard_view,
|
| 382 |
-
report_view: report_view,
|
| 383 |
-
meta: {
|
| 384 |
-
job_id: transform_job_id,
|
| 385 |
-
keyword: keyword,
|
| 386 |
-
timestamp: new Date().toISOString(),
|
| 387 |
-
status: "completed"
|
| 388 |
-
},
|
| 389 |
-
// Also include job_id at root for n8n compatibility
|
| 390 |
-
job_id: transform_job_id
|
| 391 |
-
};
|
| 392 |
}
|
| 393 |
|
| 394 |
// Error handling middleware
|
|
|
|
| 41 |
});
|
| 42 |
});
|
| 43 |
|
| 44 |
+
// Task 8.3: Diagnostic endpoint
|
| 45 |
+
app.get("/diagnostic", (req, res) => {
|
| 46 |
+
const diagnosticInfo = {
|
| 47 |
+
status: "operational",
|
| 48 |
+
timestamp: new Date().toISOString(),
|
| 49 |
+
uptime: process.uptime(),
|
| 50 |
+
components: {
|
| 51 |
+
server: {
|
| 52 |
+
status: "running",
|
| 53 |
+
port: process.env.PORT || 7860,
|
| 54 |
+
nodeVersion: process.version
|
| 55 |
+
},
|
| 56 |
+
agent: {
|
| 57 |
+
status: "ready",
|
| 58 |
+
lastError: global.lastAgentError || null,
|
| 59 |
+
lastSuccess: global.lastAgentSuccess || null
|
| 60 |
+
},
|
| 61 |
+
schema: {
|
| 62 |
+
version: "1.0.0",
|
| 63 |
+
validationEnabled: true
|
| 64 |
+
}
|
| 65 |
+
},
|
| 66 |
+
integrationPoints: {
|
| 67 |
+
n8n: {
|
| 68 |
+
endpoint: "/run",
|
| 69 |
+
status: "available"
|
| 70 |
+
},
|
| 71 |
+
wordpress: {
|
| 72 |
+
endpoint: "/api/market-research",
|
| 73 |
+
status: "available"
|
| 74 |
+
}
|
| 75 |
+
},
|
| 76 |
+
environment: {
|
| 77 |
+
hasApiKey: !!process.env.OPENCLAW_API_KEY || !!process.env.DEEPSEEK_API_KEY,
|
| 78 |
+
provider: process.env.OPENCLAW_PROVIDER || "deepseek",
|
| 79 |
+
timeout: process.env.OPENCLAW_TIMEOUT || "180000"
|
| 80 |
+
}
|
| 81 |
+
};
|
| 82 |
+
|
| 83 |
+
res.json(diagnosticInfo);
|
| 84 |
+
});
|
| 85 |
+
|
| 86 |
// Helper function to run OpenClaw agent
|
| 87 |
function runOpenClawAgent(env, payload) {
|
| 88 |
return new Promise((resolve, reject) => {
|
|
|
|
| 166 |
console.log('Calling OpenClaw agent...');
|
| 167 |
const agentResult = await runOpenClawAgent(env, { keyword });
|
| 168 |
|
| 169 |
+
// Track success for diagnostics
|
| 170 |
+
global.lastAgentSuccess = {
|
| 171 |
+
timestamp: new Date().toISOString(),
|
| 172 |
+
keyword: keyword
|
| 173 |
+
};
|
| 174 |
+
|
| 175 |
// Transform agent result to n8n format
|
| 176 |
const response = transformToN8nFormat(agentResult, keyword);
|
| 177 |
console.log('Returning transformed response');
|
|
|
|
| 179 |
|
| 180 |
} catch (error) {
|
| 181 |
console.error('OpenClaw agent error:', error.message);
|
| 182 |
+
|
| 183 |
+
// Track error for diagnostics
|
| 184 |
+
global.lastAgentError = {
|
| 185 |
+
timestamp: new Date().toISOString(),
|
| 186 |
+
keyword: keyword,
|
| 187 |
+
error: error.message
|
| 188 |
+
};
|
| 189 |
+
|
| 190 |
// Return fallback data if agent fails
|
| 191 |
const fallbackResponse = createFallbackResponse(keyword);
|
| 192 |
console.log('Returning fallback response');
|
|
|
|
| 430 |
// Also include job_id at root for n8n compatibility
|
| 431 |
job_id: fallback_job_id
|
| 432 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
}
|
| 434 |
|
| 435 |
// Error handling middleware
|