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
|
import express from "express";
import { WebSocketServer, WebSocket } from "ws";
import { IncomingMessage } from "http";
import dotenv from "dotenv";
import http from "http";
import { readFileSync } from "fs";
import { join } from "path";
import cors from "cors";
import {
handleCallConnection,
handleFrontendConnection,
} from "./sessionManager";
import functions from "./functionHandlers";
dotenv.config();
const PORT = parseInt(process.env.PORT || "8081", 10);
const PUBLIC_URL = process.env.PUBLIC_URL || "";
const app = express();
app.use(cors());
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
app.use(express.urlencoded({ extended: false }));
const twimlPath = join(__dirname, "twiml.xml");
const twimlTemplate = readFileSync(twimlPath, "utf-8");
app.get("/public-url", (req, res) => {
res.json({ publicUrl: PUBLIC_URL });
});
app.all("/twiml", (req, res) => {
const wsUrl = new URL(PUBLIC_URL);
wsUrl.protocol = "wss:";
wsUrl.pathname = `/call`;
const twimlContent = twimlTemplate.replace("{{WS_URL}}", wsUrl.toString());
res.type("text/xml").send(twimlContent);
});
// New endpoint to list available tools (schemas)
app.get("/tools", (req, res) => {
res.json(functions.map((f) => f.schema));
});
let currentCall: WebSocket | null = null;
let currentLogs: WebSocket | null = null;
wss.on("connection", (ws: WebSocket, req: IncomingMessage) => {
const url = new URL(req.url || "", `http://${req.headers.host}`);
const parts = url.pathname.split("/").filter(Boolean);
if (parts.length < 1) {
ws.close();
return;
}
const type = parts[0];
if (type === "call") {
if (currentCall) currentCall.close();
currentCall = ws;
handleCallConnection(currentCall);
} else if (type === "logs") {
if (currentLogs) currentLogs.close();
currentLogs = ws;
handleFrontendConnection(currentLogs);
} else {
ws.close();
}
});
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
|