-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (93 loc) · 2.66 KB
/
Copy pathserver.js
File metadata and controls
106 lines (93 loc) · 2.66 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
import express from "express";
import dotenv from "dotenv";
import { sqlQueryAgent } from "./lib/agent.js";
dotenv.config();
const app = express();
app.use(express.json());
// Serve static files from public directory
app.use(express.static("public"));
const PORT = process.env.PORT || 3000;
// Force restart
/**
* SQL Query Agent Endpoint
* Accepts natural language questions and returns SQL query results
*/
app.post("/ask", async (req, res) => {
try {
const { question, debug = false, maxRows = 100 } = req.body;
if (!question) {
return res.status(400).json({
success: false,
error: "Missing 'question' field in request body",
});
}
console.log(`\n📊 Processing question: "${question}"`);
const result = await sqlQueryAgent(question, {
debug,
maxRows,
maxRetries: 3,
});
if (result.success) {
console.log(`✓ Success! Returned ${result.data.rowCount} rows in ${result.data.executionTime}`);
res.json({
success: true,
answer: result.answer,
query: result.query,
data: result.data,
metadata: result.metadata,
trace: result.trace,
});
} else {
console.log(`✗ Failed: ${result.error}`);
res.status(400).json({
success: false,
error: result.error,
details: result.details,
trace: result.trace,
});
}
} catch (error) {
console.error("Server error:", error);
res.status(500).json({
success: false,
error: "Internal server error",
details: error.message,
});
}
});
/**
* Health check endpoint
*/
app.get("/health", (req, res) => {
res.json({ status: "ok", service: "SQL Query Agent" });
});
/**
* Root endpoint with API documentation
*/
app.get("/", (req, res) => {
res.json({
service: "SQL Query Agent API",
version: "1.0.0",
endpoints: {
"POST /ask": {
description: "Ask a question in natural language and get SQL query results",
body: {
question: "string (required) - Your question about the database",
debug: "boolean (optional) - Enable debug trace output",
maxRows: "number (optional) - Maximum rows to return (default: 100)",
},
example: {
question: "How many users are in the database?",
debug: false,
maxRows: 100,
},
},
"GET /health": "Health check endpoint",
},
});
});
app.listen(PORT, () => {
console.log(`\n🚀 SQL Query Agent Server running on port ${PORT}`);
console.log(`📍 http://localhost:${PORT}`);
console.log(`\n💡 Try: POST /ask with { "question": "your question here" }\n`);
});