summaryrefslogtreecommitdiffhomepage
path: root/packages/api/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/api/src/index.ts')
-rw-r--r--packages/api/src/index.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts
new file mode 100644
index 0000000..02b04b6
--- /dev/null
+++ b/packages/api/src/index.ts
@@ -0,0 +1,37 @@
+import { createBunWebSocket } from "hono/bun";
+import { agentManager, app } from "./app.js";
+
+const { upgradeWebSocket, websocket } = createBunWebSocket();
+
+app.get(
+ "/ws",
+ upgradeWebSocket((_c) => {
+ return {
+ onOpen(_event, ws) {
+ // Send current status immediately
+ ws.send(JSON.stringify({ type: "status", status: agentManager.getStatus() }));
+
+ const unsubscribe = agentManager.onEvent((event) => {
+ ws.send(JSON.stringify(event));
+ });
+
+ // Store unsubscribe fn on the raw socket for cleanup
+ (ws as unknown as { _unsub?: () => void })._unsub = unsubscribe;
+ },
+ onClose(_event, ws) {
+ const unsub = (ws as unknown as { _unsub?: () => void })._unsub;
+ if (unsub) {
+ unsub();
+ }
+ },
+ };
+ }),
+);
+
+export { app };
+
+export default {
+ port: 3000,
+ fetch: app.fetch,
+ websocket,
+};