add global.event.subscribe() to sdk

This commit is contained in:
Dax Raad
2025-11-14 12:32:43 -05:00
parent c1cf9cda6a
commit 5fc26c958a
5 changed files with 717 additions and 623 deletions

View File

@@ -40,6 +40,7 @@ import type { ContentfulStatusCode } from "hono/utils/http-status"
import { TuiEvent } from "@/cli/cmd/tui/event"
import { Snapshot } from "@/snapshot"
import { SessionSummary } from "@/session/summary"
import { GlobalBus } from "@/bus/global"
const ERRORS = {
400: {
@@ -117,6 +118,51 @@ export namespace Server {
timer.stop()
}
})
.get(
"/global/event",
describeRoute({
description: "Get events",
operationId: "global.event.subscribe",
responses: {
200: {
description: "Event stream",
content: {
"text/event-stream": {
schema: resolver(
Bus.payloads().meta({
ref: "Event",
}),
),
},
},
},
},
}),
async (c) => {
log.info("global event connected")
return streamSSE(c, async (stream) => {
stream.writeSSE({
data: JSON.stringify({
type: "server.connected",
properties: {},
}),
})
async function handler(event: any) {
await stream.writeSSE({
data: JSON.stringify(event),
})
}
GlobalBus.on("event", handler)
await new Promise<void>((resolve) => {
stream.onAbort(() => {
GlobalBus.off("event", handler)
resolve()
log.info("global event disconnected")
})
})
})
},
)
.use(async (c, next) => {
const directory = c.req.query("directory") ?? process.cwd()
return Instance.provide({