@loomwork/plugin-conversation-sse 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +39 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +106 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +669 -0
- package/dist/server.js.map +1 -0
- package/dist/sse.d.ts +29 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +107 -0
- package/dist/sse.js.map +1 -0
- package/package.json +61 -3
- package/src/index.ts +7 -0
- package/src/server.ts +806 -0
- package/src/sse.ts +106 -0
package/src/sse.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type * as http from "node:http";
|
|
2
|
+
|
|
3
|
+
// ── SSE framing and per-connection backpressure ───────────────────────────────────────────────────
|
|
4
|
+
//
|
|
5
|
+
// A socket that cannot keep up must not make the server buffer without bound. `write` returning false
|
|
6
|
+
// means the kernel buffer is full: from then on frames queue here, up to `maxPendingBytes`, and a
|
|
7
|
+
// connection that exceeds it is CLOSED (the client reconnects with Last-Event-ID and the log supplies
|
|
8
|
+
// what it missed — durable messages lose nothing). Live reply deltas are lossy by design, so while
|
|
9
|
+
// blocked they are COALESCED (one growing delta per turn) instead of queued frame by frame.
|
|
10
|
+
|
|
11
|
+
/** One SSE frame. `data` is JSON on one line (JSON.stringify escapes every newline). */
|
|
12
|
+
export function frame(event: string, data: unknown, id?: string): string {
|
|
13
|
+
return `${id !== undefined ? `id: ${id}\n` : ""}event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Build = (turn: number, text: string) => string;
|
|
17
|
+
|
|
18
|
+
export class SseSink {
|
|
19
|
+
private queue: string[] = [];
|
|
20
|
+
private pending = 0;
|
|
21
|
+
private blocked = false;
|
|
22
|
+
/** Live (lossy) deltas coalesced while the socket is backed up: one growing entry per channel (reply text, reasoning). */
|
|
23
|
+
private coalesced = new Map<string, { turn: number; text: string; build: Build }>();
|
|
24
|
+
private closed = false;
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
private readonly res: http.ServerResponse,
|
|
28
|
+
private readonly maxPendingBytes: number,
|
|
29
|
+
private readonly onOverflow: () => void
|
|
30
|
+
) {
|
|
31
|
+
res.on("drain", () => this.flush());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get isClosed(): boolean {
|
|
35
|
+
return this.closed;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
close(): void {
|
|
39
|
+
this.closed = true;
|
|
40
|
+
this.queue = [];
|
|
41
|
+
this.coalesced.clear();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A durable frame: ordered, never dropped (a slow client is closed instead). */
|
|
45
|
+
send(text: string): void {
|
|
46
|
+
if (this.closed) return;
|
|
47
|
+
if (!this.blocked) {
|
|
48
|
+
this.write(text);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.enqueue(text);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A live delta (reply text by default; reasoning on its own `channel`): coalesced while the socket is backed up. */
|
|
55
|
+
delta(turn: number, text: string, build: Build, channel = "delta"): void {
|
|
56
|
+
if (this.closed) return;
|
|
57
|
+
if (!this.blocked) {
|
|
58
|
+
this.write(build(turn, text));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const cur = this.coalesced.get(channel);
|
|
62
|
+
if (cur !== undefined && cur.turn !== turn) this.settle(channel, cur);
|
|
63
|
+
const base = this.coalesced.get(channel);
|
|
64
|
+
this.coalesced.set(channel, { turn, text: (base?.text ?? "") + text, build });
|
|
65
|
+
this.pending += text.length;
|
|
66
|
+
if (this.pending > this.maxPendingBytes) this.overflow();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private settle(channel: string, c: { turn: number; text: string; build: Build }): void {
|
|
70
|
+
this.coalesced.delete(channel);
|
|
71
|
+
this.pending -= c.text.length;
|
|
72
|
+
this.enqueue(c.build(c.turn, c.text));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private enqueue(text: string): void {
|
|
76
|
+
this.queue.push(text);
|
|
77
|
+
this.pending += text.length;
|
|
78
|
+
if (this.pending > this.maxPendingBytes) this.overflow();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private overflow(): void {
|
|
82
|
+
if (this.closed) return;
|
|
83
|
+
this.close();
|
|
84
|
+
this.onOverflow();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private write(text: string): void {
|
|
88
|
+
if (!this.res.write(text)) this.blocked = true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private flush(): void {
|
|
92
|
+
if (this.closed) return;
|
|
93
|
+
this.blocked = false;
|
|
94
|
+
while (this.queue.length > 0 && !this.blocked) {
|
|
95
|
+
const next = this.queue.shift() as string;
|
|
96
|
+
this.pending -= next.length;
|
|
97
|
+
this.write(next);
|
|
98
|
+
}
|
|
99
|
+
for (const [channel, c] of [...this.coalesced]) {
|
|
100
|
+
if (this.blocked) break;
|
|
101
|
+
this.coalesced.delete(channel);
|
|
102
|
+
this.pending -= c.text.length;
|
|
103
|
+
this.write(c.build(c.turn, c.text));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|