-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
98 lines (80 loc) · 2.26 KB
/
Copy pathserver.js
File metadata and controls
98 lines (80 loc) · 2.26 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
const express = require("express");
const app = express();
app.use(express.json());
const {
GATEWAY_API_KEY,
DATASTAX_LANGFLOW_URL,
LANGFLOW_TENANT_ID,
FLOW_ID,
ASTRA_ORG_ID,
APPLICATION_TOKEN
} = process.env;
app.get("/", (_req, res) => {
res.send("gateway alive");
});
app.get("/health", (_req, res) => {
res.json({ ok: true });
});
app.post("/run", async (req, res) => {
const received = (req.header("x-api-key") || "").trim().replace(/:$/, "");
if (received !== GATEWAY_API_KEY) {
return res.status(401).json({ error: "Unauthorized" });
}
const { raw_user_input, session_id } = req.body || {};
if (!raw_user_input || typeof raw_user_input !== "string") {
return res.status(400).json({ error: "Missing required field: raw_user_input" });
}
console.log("Incoming raw_user_input:", raw_user_input);
const url =
`${DATASTAX_LANGFLOW_URL}/lf/${LANGFLOW_TENANT_ID}/api/v1/run/${FLOW_ID}?stream=false`;
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${APPLICATION_TOKEN}`,
"X-DataStax-Current-Org": ASTRA_ORG_ID
},
body: JSON.stringify({
input_value: raw_user_input,
input_type: "chat",
output_type: "chat",
...(session_id ? { session_id } : {})
})
});
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
data = { raw: text };
}
console.log("Langflow status:", response.status);
console.log("Langflow response:", text);
if (!response.ok) {
return res.status(response.status).json({
error: "Langflow API error",
status: response.status,
response: data
});
}
const answer =
data?.outputs?.[0]?.outputs?.[0]?.results?.message?.text ??
data?.outputs?.[0]?.outputs?.[0]?.results?.message ??
"";
return res.json({
answer,
raw: data
});
} catch (err) {
console.error("Gateway failure:", err);
return res.status(500).json({
error: "Gateway failure",
details: String(err)
});
}
});
const port = process.env.PORT || 10000;
app.listen(port, () => {
console.log(`Gateway running on port ${port}`);
});