agentfeed 0.1.1 → 0.1.3

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.
Files changed (2) hide show
  1. package/dist/sse-client.js +87 -47
  2. package/package.json +1 -1
@@ -1,60 +1,100 @@
1
+ import http from "node:http";
2
+ import https from "node:https";
1
3
  export function connectSSE(url, apiKey, onEvent, onError) {
2
4
  let aborted = false;
3
- const controller = new AbortController();
4
- const run = async () => {
5
- while (!aborted) {
6
- try {
7
- const res = await fetch(url, {
8
- headers: { Authorization: `Bearer ${apiKey}` },
9
- signal: controller.signal,
10
- });
11
- if (!res.ok || !res.body) {
12
- throw new Error(`SSE connect failed: ${res.status}`);
13
- }
14
- const reader = res.body.getReader();
15
- const decoder = new TextDecoder();
16
- let buffer = "";
17
- let currentEvent = { type: "message", data: "" };
18
- while (!aborted) {
19
- const { done, value } = await reader.read();
20
- if (done)
21
- break;
22
- buffer += decoder.decode(value, { stream: true });
23
- const lines = buffer.split("\n");
24
- buffer = lines.pop() ?? "";
25
- for (const line of lines) {
26
- if (line.startsWith("event: ")) {
27
- currentEvent.type = line.slice(7);
28
- }
29
- else if (line.startsWith("data: ")) {
30
- currentEvent.data = line.slice(6);
31
- }
32
- else if (line.startsWith("id: ")) {
33
- currentEvent.id = line.slice(4);
34
- }
35
- else if (line === "") {
36
- if (currentEvent.data) {
37
- onEvent({ ...currentEvent });
38
- }
39
- currentEvent = { type: "message", data: "" };
5
+ let reconnecting = false;
6
+ let currentReq = null;
7
+ const connect = () => {
8
+ if (aborted)
9
+ return;
10
+ reconnecting = false;
11
+ const parsed = new URL(url);
12
+ const isHttps = parsed.protocol === "https:";
13
+ const options = {
14
+ hostname: parsed.hostname,
15
+ port: parsed.port || (isHttps ? 443 : 80),
16
+ path: parsed.pathname + parsed.search,
17
+ method: "GET",
18
+ headers: {
19
+ Authorization: `Bearer ${apiKey}`,
20
+ Accept: "text/event-stream",
21
+ "Cache-Control": "no-cache",
22
+ Connection: "keep-alive",
23
+ },
24
+ // Disable connection pooling to avoid agent timeouts
25
+ agent: false,
26
+ };
27
+ const mod = isHttps ? https : http;
28
+ const req = mod.request(options, (res) => {
29
+ if (res.statusCode !== 200) {
30
+ res.resume();
31
+ onError(new Error(`SSE connect failed: ${res.statusCode}`));
32
+ scheduleReconnect();
33
+ return;
34
+ }
35
+ // Keep TCP connection alive
36
+ res.socket?.setKeepAlive(true, 10000);
37
+ res.socket?.setTimeout(0);
38
+ res.setEncoding("utf-8");
39
+ let buffer = "";
40
+ let currentEvent = { type: "message", data: "" };
41
+ res.on("data", (chunk) => {
42
+ buffer += chunk;
43
+ const lines = buffer.split("\n");
44
+ buffer = lines.pop() ?? "";
45
+ for (const line of lines) {
46
+ if (line.startsWith("event: ")) {
47
+ currentEvent.type = line.slice(7);
48
+ }
49
+ else if (line.startsWith("data: ")) {
50
+ currentEvent.data = line.slice(6);
51
+ }
52
+ else if (line.startsWith("id: ")) {
53
+ currentEvent.id = line.slice(4);
54
+ }
55
+ else if (line === "") {
56
+ if (currentEvent.data || currentEvent.type === "heartbeat") {
57
+ onEvent({ ...currentEvent });
40
58
  }
59
+ currentEvent = { type: "message", data: "" };
41
60
  }
42
61
  }
62
+ });
63
+ res.on("end", () => {
64
+ if (!aborted) {
65
+ console.log("SSE stream ended, reconnecting...");
66
+ scheduleReconnect();
67
+ }
68
+ });
69
+ res.on("error", (err) => {
70
+ if (!aborted) {
71
+ onError(err);
72
+ scheduleReconnect();
73
+ }
74
+ });
75
+ });
76
+ req.on("error", (err) => {
77
+ if (!aborted) {
78
+ onError(err);
79
+ scheduleReconnect();
43
80
  }
44
- catch (err) {
45
- if (aborted)
46
- return;
47
- onError(err instanceof Error ? err : new Error(String(err)));
48
- // Reconnect after delay
49
- await new Promise((r) => setTimeout(r, 5000));
50
- }
51
- }
81
+ });
82
+ // Disable request timeout
83
+ req.setTimeout(0);
84
+ req.end();
85
+ currentReq = req;
86
+ };
87
+ const scheduleReconnect = () => {
88
+ if (aborted || reconnecting)
89
+ return;
90
+ reconnecting = true;
91
+ setTimeout(connect, 5000);
52
92
  };
53
- run();
93
+ connect();
54
94
  return {
55
95
  close: () => {
56
96
  aborted = true;
57
- controller.abort();
97
+ currentReq?.destroy();
58
98
  },
59
99
  };
60
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentfeed",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Worker daemon for AgentFeed - watches feeds and wakes AI agents via claude -p",
5
5
  "type": "module",
6
6
  "bin": {