fix: resolve duplicate endpoint definitions and nested route bug
This commit is contained in:
141
server.js
141
server.js
@@ -229,152 +229,15 @@ app.patch('/api/tasks/:id', (req, res) => {
|
||||
} catch (wikiErr) {
|
||||
console.error('wiki_creation_error', wikiErr);
|
||||
}
|
||||
|
||||
// Agents endpoint
|
||||
app.get('/api/agents', (req, res) => {
|
||||
try {
|
||||
const agents = [];
|
||||
|
||||
if (fs.existsSync(AGENTS_DIR)) {
|
||||
const agentDirs = fs.readdirSync(AGENTS_DIR).filter(d => {
|
||||
return fs.statSync(path.join(AGENTS_DIR, d)).isDirectory();
|
||||
});
|
||||
|
||||
agentDirs.forEach(agentName => {
|
||||
const agentPath = path.join(AGENTS_DIR, agentName);
|
||||
const workspacePath = path.join(agentPath, 'workspace');
|
||||
|
||||
const agent = {
|
||||
name: agentName,
|
||||
status: 'active',
|
||||
currentTask: null,
|
||||
tools: [],
|
||||
files: [],
|
||||
permissions: []
|
||||
};
|
||||
|
||||
// Read workspace files
|
||||
if (fs.existsSync(workspacePath)) {
|
||||
const files = fs.readdirSync(workspacePath);
|
||||
agent.files = files.filter(f => f.endsWith('.md'));
|
||||
|
||||
// Read MEMORY.md for tools
|
||||
const memoryPath = path.join(workspacePath, 'MEMORY.md');
|
||||
if (fs.existsSync(memoryPath)) {
|
||||
const memory = fs.readFileSync(memoryPath, 'utf8');
|
||||
// Extract tools from memory
|
||||
const toolMatches = memory.match(/##\s+Tools([\s\S]*?)(?=##|$)/i);
|
||||
if (toolMatches) {
|
||||
agent.tools = toolMatches[1].split('\n')
|
||||
.filter(line => line.trim().startsWith('-'))
|
||||
.map(line => line.replace(/^-\s*/, '').trim());
|
||||
}
|
||||
}
|
||||
|
||||
// Read HEARTBEAT.md for current task
|
||||
const heartbeatPath = path.join(workspacePath, 'HEARTBEAT.md');
|
||||
if (fs.existsSync(heartbeatPath)) {
|
||||
const heartbeat = fs.readFileSync(heartbeatPath, 'utf8');
|
||||
const taskMatch = heartbeat.match(/Current Task:\s*(.+)/i);
|
||||
if (taskMatch) {
|
||||
agent.currentTask = taskMatch[1].trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
agents.push(agent);
|
||||
});
|
||||
}
|
||||
|
||||
res.json(agents);
|
||||
} catch (err) {
|
||||
console.error('Error reading agents:', err);
|
||||
res.status(500).json({ error: 'failed_to_fetch_agents' });
|
||||
}
|
||||
});
|
||||
|
||||
// Usage endpoint
|
||||
app.get('/api/usage', (req, res) => {
|
||||
try {
|
||||
const usage = {
|
||||
providers: [],
|
||||
lastUpdated: new Date().toISOString()
|
||||
};
|
||||
|
||||
// Read OpenClaw config
|
||||
if (fs.existsSync(OPENCLAW_CONFIG)) {
|
||||
const config = JSON.parse(fs.readFileSync(OPENCLAW_CONFIG, 'utf8'));
|
||||
|
||||
// Extract provider information
|
||||
if (config.models) {
|
||||
const providerMap = {};
|
||||
|
||||
Object.entries(config.models).forEach(([modelName, modelConfig]) => {
|
||||
const provider = modelConfig.provider || 'unknown';
|
||||
|
||||
if (!providerMap[provider]) {
|
||||
providerMap[provider] = {
|
||||
name: provider,
|
||||
models: [],
|
||||
quota: {
|
||||
requests: 0,
|
||||
tokens: 0,
|
||||
limit: 'unlimited'
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
providerMap[provider].models.push({
|
||||
name: modelName,
|
||||
type: modelConfig.type || 'chat',
|
||||
contextWindow: modelConfig.context_window || 'unknown'
|
||||
});
|
||||
});
|
||||
|
||||
usage.providers = Object.values(providerMap);
|
||||
}
|
||||
}
|
||||
|
||||
res.json(usage);
|
||||
} catch (err) {
|
||||
console.error('Error reading usage:', err);
|
||||
res.status(500).json({ error: 'failed_to_fetch_usage' });
|
||||
}
|
||||
});
|
||||
|
||||
// Heartbeat endpoint for agents
|
||||
app.get('/api/heartbeat/:agent', (req, res) => {
|
||||
const agent = req.params.agent;
|
||||
|
||||
db.all(
|
||||
'SELECT * FROM tasks WHERE assignee = ? AND status IN (?, ?, ?) ORDER BY priority DESC, created_at ASC',
|
||||
[agent, 'Todo', 'In Progress', 'Review'],
|
||||
(err, rows) => {
|
||||
if (err) {
|
||||
return res.status(500).json({ error: 'failed_to_fetch_tasks' });
|
||||
}
|
||||
|
||||
const tasks = rows.map(normalizeTask);
|
||||
res.json({
|
||||
agent,
|
||||
pending_tasks: tasks.length,
|
||||
tasks
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
broadcast('task_updated', task);
|
||||
return res.json(task);
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Agents endpoint
|
||||
app.get('/api/agents', (req, res) => {
|
||||
try {
|
||||
const agents = [];
|
||||
|
||||
Reference in New Issue
Block a user