Download server/websocket.ts from truegleai/tinker: direct link, hf CLI and curl.
- Browser
- Download file 2.31 kB
-
https://huggingface.co/spaces/truegleai/tinker/resolve/main/server/websocket.ts
- Command line
-
hf download hf://spaces/truegleai/tinker/server/websocket.ts
-
curl -L -o websocket.ts https://huggingface.co/spaces/truegleai/tinker/resolve/main/server/websocket.ts
2.31 kB
| import { createServer } from "http"; | |
| import { Server } from "socket.io"; | |
| const PORT = parseInt(process.env.WEBSOCKET_PORT || "3001", 10); | |
| const httpServer = createServer((req, res) => { | |
| if (req.url === "/health") { | |
| res.writeHead(200, { "Content-Type": "application/json" }); | |
| res.end(JSON.stringify({ status: "ok", port: PORT })); | |
| return; | |
| } | |
| res.writeHead(404); | |
| res.end(); | |
| }); | |
| const io = new Server(httpServer, { | |
| cors: { | |
| origin: process.env.NEXTAUTH_URL || "http://localhost:3000", | |
| methods: ["GET", "POST"], | |
| credentials: true, | |
| }, | |
| pingTimeout: 60000, | |
| pingInterval: 25000, | |
| }); | |
| interface AgentEvent { | |
| type: "scan_start" | "scan_complete" | "deal_found" | "deal_analyzed" | "negotiation_update" | "transaction_update" | "queue_update" | "error" | "status_change"; | |
| data: Record<string, unknown>; | |
| timestamp: string; | |
| } | |
| const connectedClients = new Map<string, Date>(); | |
| io.on("connection", (socket) => { | |
| connectedClients.set(socket.id, new Date()); | |
| console.log(`[WebSocket] Client connected: ${socket.id}`); | |
| socket.emit("connected", { | |
| clientId: socket.id, | |
| timestamp: new Date().toISOString(), | |
| serverUptime: process.uptime(), | |
| }); | |
| socket.on("subscribe", (room: string) => { | |
| socket.join(room); | |
| console.log(`[WebSocket] ${socket.id} subscribed to: ${room}`); | |
| }); | |
| socket.on("agent_command", (command: { action: string; payload?: unknown }) => { | |
| console.log(`[WebSocket] Command from ${socket.id}:`, command); | |
| io.emit("agent_command_ack", { | |
| command, | |
| received: new Date().toISOString(), | |
| }); | |
| }); | |
| socket.on("disconnect", () => { | |
| connectedClients.delete(socket.id); | |
| console.log(`[WebSocket] Client disconnected: ${socket.id}`); | |
| }); | |
| }); | |
| export function emitAgentEvent(event: AgentEvent): void { | |
| io.emit("agent_event", { | |
| ...event, | |
| timestamp: event.timestamp || new Date().toISOString(), | |
| }); | |
| } | |
| export function emitToRoom(room: string, event: AgentEvent): void { | |
| io.to(room).emit("agent_event", { | |
| ...event, | |
| timestamp: event.timestamp || new Date().toISOString(), | |
| }); | |
| } | |
| export function getConnectedCount(): number { | |
| return io.sockets.sockets.size; | |
| } | |
| httpServer.listen(PORT, () => { | |
| console.log(`[WebSocket] Server running on port ${PORT}`); | |
| }); | |
| export { io, httpServer }; | |