Upload 2 files
Browse files
.env
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OpenClaw Gateway Key
|
| 2 |
+
OPENCLAW_GATE_KEY=openclaw_ZJLv3akrp511lCANb4bcfREvfLsaTf5KGIx4TeheWLQ
|
| 3 |
+
|
| 4 |
+
# Server Port
|
| 5 |
+
PORT=3000
|
server.js
CHANGED
|
@@ -108,24 +108,24 @@ app.post("/run", async (req, res) => {
|
|
| 108 |
});
|
| 109 |
|
| 110 |
const { task = "market_research", provider, model, ...payload } = req.body;
|
| 111 |
-
|
| 112 |
-
if (!TASKS.has(task)) {
|
| 113 |
-
return res.status(400).json({ error: "Unknown task" });
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
const providersToTry = provider
|
| 117 |
-
? [provider, ...Object.keys(PROVIDERS).filter(p => p !== provider)]
|
| 118 |
-
: Object.keys(PROVIDERS);
|
| 119 |
-
|
| 120 |
-
let lastError;
|
| 121 |
-
|
| 122 |
-
for (const p of providersToTry) {
|
| 123 |
-
const pool = PROVIDERS[p];
|
| 124 |
-
if (!pool || pool.length === 0) continue;
|
| 125 |
-
|
| 126 |
-
for (let i = 0; i < pool.length; i++) {
|
| 127 |
-
const apiKey = randomKey(pool);
|
| 128 |
-
|
| 129 |
const env = {
|
| 130 |
...process.env,
|
| 131 |
OPENCLAW_PROVIDER: p,
|
|
@@ -134,24 +134,94 @@ const env = {
|
|
| 134 |
OPENCLAW_TASK: task,
|
| 135 |
OPENCLAW_TIMEOUT: "180000"
|
| 136 |
};
|
| 137 |
-
|
| 138 |
-
try {
|
| 139 |
-
const result = await runOpenClaw(env, payload);
|
| 140 |
-
return res.json(result);
|
| 141 |
-
} catch (err) {
|
| 142 |
-
lastError = err;
|
| 143 |
-
if (!isRateLimit(err)) break;
|
| 144 |
-
}
|
| 145 |
-
}
|
| 146 |
-
}
|
| 147 |
-
|
| 148 |
res.status(500).json({
|
| 149 |
error: "All providers failed",
|
| 150 |
details: lastError?.message,
|
| 151 |
keyword: req.body.keyword,
|
| 152 |
timestamp: new Date().toISOString()
|
| 153 |
});
|
| 154 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
/* ===============================
|
| 157 |
6. EXECUTION + JSON PARSE
|
|
@@ -180,20 +250,26 @@ function runOpenClaw(env, payload) {
|
|
| 180 |
stderr += data.toString();
|
| 181 |
});
|
| 182 |
|
| 183 |
-
proc.on("close", code => {
|
| 184 |
-
if (code !== 0) {
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
// SEND INPUT TO AGENT
|
| 199 |
proc.stdin.write(JSON.stringify(payload));
|
|
@@ -217,6 +293,7 @@ function isRateLimit(err) {
|
|
| 217 |
7. START
|
| 218 |
================================ */
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
});
|
|
|
|
|
|
| 108 |
});
|
| 109 |
|
| 110 |
const { task = "market_research", provider, model, ...payload } = req.body;
|
| 111 |
+
|
| 112 |
+
if (!TASKS.has(task)) {
|
| 113 |
+
return res.status(400).json({ error: "Unknown task" });
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
const providersToTry = provider
|
| 117 |
+
? [provider, ...Object.keys(PROVIDERS).filter(p => p !== provider)]
|
| 118 |
+
: Object.keys(PROVIDERS);
|
| 119 |
+
|
| 120 |
+
let lastError;
|
| 121 |
+
|
| 122 |
+
for (const p of providersToTry) {
|
| 123 |
+
const pool = PROVIDERS[p];
|
| 124 |
+
if (!pool || pool.length === 0) continue;
|
| 125 |
+
|
| 126 |
+
for (let i = 0; i < pool.length; i++) {
|
| 127 |
+
const apiKey = randomKey(pool);
|
| 128 |
+
|
| 129 |
const env = {
|
| 130 |
...process.env,
|
| 131 |
OPENCLAW_PROVIDER: p,
|
|
|
|
| 134 |
OPENCLAW_TASK: task,
|
| 135 |
OPENCLAW_TIMEOUT: "180000"
|
| 136 |
};
|
| 137 |
+
|
| 138 |
+
try {
|
| 139 |
+
const result = await runOpenClaw(env, payload);
|
| 140 |
+
return res.json(result);
|
| 141 |
+
} catch (err) {
|
| 142 |
+
lastError = err;
|
| 143 |
+
if (!isRateLimit(err)) break;
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
res.status(500).json({
|
| 149 |
error: "All providers failed",
|
| 150 |
details: lastError?.message,
|
| 151 |
keyword: req.body.keyword,
|
| 152 |
timestamp: new Date().toISOString()
|
| 153 |
});
|
| 154 |
+
});
|
| 155 |
+
|
| 156 |
+
// Add specific market research endpoint for n8n compatibility
|
| 157 |
+
app.post("/api/market-research", async (req, res) => {
|
| 158 |
+
console.log('OpenClaw: Market research request:', {
|
| 159 |
+
keyword: req.body.keyword,
|
| 160 |
+
timestamp: new Date().toISOString()
|
| 161 |
+
});
|
| 162 |
+
|
| 163 |
+
const { keyword, api_key, provider = "deepseek" } = req.body;
|
| 164 |
+
|
| 165 |
+
if (!keyword) {
|
| 166 |
+
return res.status(400).json({ error: "keyword is required" });
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
// Use API key from request or fallback to environment
|
| 170 |
+
let providersToTry;
|
| 171 |
+
let apiKeys = {};
|
| 172 |
+
|
| 173 |
+
if (api_key) {
|
| 174 |
+
// Use API key from n8n request
|
| 175 |
+
console.log('OpenClaw: Using API key from request');
|
| 176 |
+
apiKeys[provider] = [api_key];
|
| 177 |
+
providersToTry = [provider];
|
| 178 |
+
} else {
|
| 179 |
+
// Fallback to environment variables (for local testing)
|
| 180 |
+
console.log('OpenClaw: Using API keys from environment');
|
| 181 |
+
providersToTry = Object.keys(PROVIDERS);
|
| 182 |
+
apiKeys = PROVIDERS;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
let lastError;
|
| 186 |
+
|
| 187 |
+
for (const p of providersToTry) {
|
| 188 |
+
const pool = apiKeys[p];
|
| 189 |
+
if (!pool || pool.length === 0) {
|
| 190 |
+
console.log(`OpenClaw: No API keys for provider ${p}`);
|
| 191 |
+
continue;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
for (let i = 0; i < pool.length; i++) {
|
| 195 |
+
const apiKey = pool[i];
|
| 196 |
+
|
| 197 |
+
const env = {
|
| 198 |
+
...process.env,
|
| 199 |
+
OPENCLAW_PROVIDER: p,
|
| 200 |
+
OPENCLAW_API_KEY: apiKey,
|
| 201 |
+
OPENCLAW_TASK: "market_research",
|
| 202 |
+
OPENCLAW_TIMEOUT: "180000"
|
| 203 |
+
};
|
| 204 |
+
|
| 205 |
+
try {
|
| 206 |
+
console.log(`OpenClaw: Trying provider ${p} with key ${apiKey.substring(0, 10)}...`);
|
| 207 |
+
const result = await runOpenClaw(env, { keyword });
|
| 208 |
+
console.log('OpenClaw: Success with provider', p);
|
| 209 |
+
return res.json(result);
|
| 210 |
+
} catch (err) {
|
| 211 |
+
lastError = err;
|
| 212 |
+
console.error('OpenClaw: Provider failed:', p, err.message);
|
| 213 |
+
if (!isRateLimit(err)) break;
|
| 214 |
+
}
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
res.status(500).json({
|
| 219 |
+
error: "All providers failed",
|
| 220 |
+
details: lastError?.message || "No API keys configured",
|
| 221 |
+
keyword: keyword,
|
| 222 |
+
timestamp: new Date().toISOString()
|
| 223 |
+
});
|
| 224 |
+
});
|
| 225 |
|
| 226 |
/* ===============================
|
| 227 |
6. EXECUTION + JSON PARSE
|
|
|
|
| 250 |
stderr += data.toString();
|
| 251 |
});
|
| 252 |
|
| 253 |
+
proc.on("close", code => {
|
| 254 |
+
if (code !== 0) {
|
| 255 |
+
console.error('OpenClaw: Agent exited with code:', code);
|
| 256 |
+
console.error('OpenClaw: stderr:', stderr);
|
| 257 |
+
return reject(
|
| 258 |
+
new Error(stderr || `Agent exited with code ${code}`)
|
| 259 |
+
);
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
try {
|
| 263 |
+
console.log('OpenClaw: Agent stdout (first 500 chars):', stdout.substring(0, 500));
|
| 264 |
+
const json = JSON.parse(stdout);
|
| 265 |
+
console.log('OpenClaw: JSON parsed successfully');
|
| 266 |
+
resolve(json);
|
| 267 |
+
} catch (err) {
|
| 268 |
+
console.error('OpenClaw: JSON parse error:', err.message);
|
| 269 |
+
console.error('OpenClaw: Raw stdout:', stdout);
|
| 270 |
+
reject(new Error("Invalid JSON from OpenClaw agent: " + err.message));
|
| 271 |
+
}
|
| 272 |
+
});
|
| 273 |
|
| 274 |
// SEND INPUT TO AGENT
|
| 275 |
proc.stdin.write(JSON.stringify(payload));
|
|
|
|
| 293 |
7. START
|
| 294 |
================================ */
|
| 295 |
|
| 296 |
+
const PORT = process.env.PORT || 3000;
|
| 297 |
+
app.listen(PORT, () => {
|
| 298 |
+
console.log(`🚀 OpenClaw Agent Gateway running on port ${PORT}`);
|
| 299 |
+
});
|