Files
TREK/server/src/nest/common/sse-stream.ts
T
Maurice b64da26651 feat(assistant): trip assistant chat with your own AI connection
Adds an in-app assistant that reads and plans the open trip. Users connect
their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or
OpenRouter key) or use the one the admin set up, including a custom
OpenAI-compatible server. It is an addon and only shows while a connection
is usable.

The chat runs on the existing MCP tools, pinned to the trip. Every change is
held as a change card the user applies or discards, and applied changes can
be undone. On the desktop it is a floating window that can be moved and
resized; on the phone it is fullscreen. Search results show as place cards
that can go onto a day, weather lookups as a strip, proposed places as
ghost markers on the map with a before/after switch, and an answer can be
shared into the trip chat. Booking files can be dropped in and are read
into change cards.
2026-10-22 21:30:12 +00:00

75 lines
2.4 KiB
TypeScript

import type { Response } from 'express';
/** How often an idle stream sends a comment, below the 60 s most reverse proxies wait for a byte. */
const SSE_KEEPALIVE_MS = 15_000;
/**
* A server-sent event stream a handler writes through `@Res()`. Writes after
* the stream ended, or after the client went away, are dropped: whoever
* produces the events never has to ask first.
*/
export interface SseStream {
/** One `data:` event. */
send(data: string): void;
/** One comment line, which a client ignores and a proxy counts as traffic. */
comment(text: string): void;
end(): void;
readonly open: boolean;
}
/**
* Opens `res` as an event stream: status and headers go out at once, so the
* client sees the stream start before the first event. The content type is set
* before anything is written, which is what keeps the compression middleware
* from buffering it (its filter reads that header), and `X-Accel-Buffering`
* does the same for nginx.
*/
export function openSseStream(res: Response): SseStream {
res.status(200);
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
res.setHeader('Cache-Control', 'no-cache, no-transform');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no');
res.flushHeaders();
const writable = () => !res.writableEnded && !res.destroyed;
return {
send(data) {
if (writable()) res.write(`data: ${data}\n\n`);
},
comment(text) {
if (writable()) res.write(`: ${text}\n\n`);
},
end() {
if (writable()) res.end();
},
get open() {
return writable();
},
};
}
/**
* Sends a keepalive comment every `intervalMs` while the stream is open. The
* answer stops it; it also stops on its own once the stream ended.
*/
export function armKeepalive(stream: SseStream, intervalMs = SSE_KEEPALIVE_MS): () => void {
const timer = setInterval(() => {
if (stream.open) stream.comment('keepalive');
else clearInterval(timer);
}, intervalMs);
timer.unref();
return () => clearInterval(timer);
}
/**
* Calls `onGone` once if the client disconnects before the response ended.
* The response's `close` is the signal, not the request's: on Node 22 the
* request closes as soon as its body has been read, which is long before a
* streamed answer is done.
*/
export function onClientGone(res: Response, onGone: () => void): void {
res.once('close', () => {
if (!res.writableEnded) onGone();
});
}