-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
167 lines (153 loc) · 4.72 KB
/
server.js
File metadata and controls
167 lines (153 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const express = require('express');
const { Pool } = require('pg');
const fetch = require('node-fetch');
const { exec } = require('child_process');
const app = express();
app.use(express.json());
// Set this in your environment variables
const TIGER_DATA_URL = process.env.TIGER_DATA_URL;
const pool = new Pool({ connectionString: TIGER_DATA_URL });
// --- AGENT TOOL IMPLEMENTATIONS ---
/**
* Tool 1: Calls any external API.
*/
async function callExternalAPI(url) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`API returned status ${response.status}`);
const data = await response.json();
return { success: true, data: data };
} catch (error) {
return { success: false, error: error.message };
}
}
/**
* Tool 2: Executes a *safe* SQL query.
*/
async function executeSQL(sqlQuery) {
const forbiddenCommands = ['DROP DATABASE', 'GRANT', 'REVOKE'];
if (forbiddenCommands.some(cmd => sqlQuery.toUpperCase().includes(cmd))) {
return { success: false, error: 'Forbidden SQL command.' };
}
try {
const client = await pool.connect();
const result = await client.query(sqlQuery);
client.release();
return { success: true, result: result.rows };
} catch (error) {
return { success: false, error: error.message };
}
}
/**
* Tool 3: Creates a Tiger Data Zero-Copy Fork.
*/
async function createZeroCopyFork(forkName, sourceService) {
const command = `tiger service fork create --name ${forkName} --from ${sourceService}`;
return new Promise((resolve) => {
exec(command, (error, stdout, stderr) => {
if (error) { resolve({ success: false, error: stderr }); }
else { resolve({ success: true, output: `Fork ${forkName} created.` }); }
});
});
}
/**
* Tool 4: Deletes a Tiger Data Fork.
*/
async function deleteFork(forkName) {
const command = `tiger service fork delete --name ${forkName}`;
return new Promise((resolve) => {
exec(command, (error, stdout, stderr) => {
if (error) { resolve({ success: false, error: stderr }); }
else { resolve({ success: true, output: `Fork ${forkName} deleted.` }); }
});
});
}
/**
* **NEW TOOL 5: Gets schema info to power your "Schema Viewer" tab.**
*/
async function getSchemaInfo(tableName) {
const sqlQuery = `
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = $1
ORDER BY ordinal_position;
`;
try {
const client = await pool.connect();
const result = await client.query(sqlQuery, [tableName]);
client.release();
return { success: true, schema: result.rows };
} catch (error) {
return { success: false, error: error.message };
}
}
/**
* **NEW TOOL 6: Gets monitoring data to power your "Dashboard" tab.**
*/
async function getMonitoringData() {
// This query gets the daily average price, perfect for a chart.
const sqlQuery = `
SELECT
DATE_TRUNC('day', timestamp) AS date,
AVG(price) AS average_price
FROM bitcoin_prices
GROUP BY date
ORDER BY date ASC;
`;
try {
const client = await pool.connect();
const result = await client.query(sqlQuery);
client.release();
// Formats data perfectly for react-charts
const chartData = [
{
label: 'Daily Average Price',
data: result.rows.map(row => ({
primary: new Date(row.date),
secondary: parseFloat(row.average_price)
}))
}
];
return { success: true, chartData: chartData };
} catch (error) {
return { success: false, error: error.message };
}
}
// --- The Single API Endpoint for All Agents ---
app.post('/api/agent-tools', async (req, res) => {
const { action, params } = req.body;
let result;
console.log(`[API] Received action: ${action} with params:`, params);
try {
switch (action) {
case 'call_api':
result = await callExternalAPI(params.url);
break;
case 'execute_sql':
result = await executeSQL(params.sqlQuery);
break;
T case 'create_zero_copy_fork':
result = await createZeroCopyFork(params.forkName, params.sourceService);
break;
case 'delete_fork':
result = await deleteFork(params.forkName);
break;
// **NEW ENDPOINTS**
case 'get_schema_info':
result = await getSchemaInfo(params.tableName);
break;
case 'get_monitoring_data':
result = await getMonitoringData();
break;
default:
result = { success: false, error: 'Unknown action' };
}
res.json(result);
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Agents' "Hands" API listening on http://localhost:${PORT}`);
});