agentfeed 0.1.1 → 0.1.2

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 +68 -47
  2. package/package.json +1 -1
@@ -1,60 +1,81 @@
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 currentReq = null;
6
+ const connect = () => {
7
+ if (aborted)
8
+ return;
9
+ const parsed = new URL(url);
10
+ const mod = parsed.protocol === "https:" ? https : http;
11
+ const req = mod.request(url, {
12
+ headers: {
13
+ Authorization: `Bearer ${apiKey}`,
14
+ Accept: "text/event-stream",
15
+ "Cache-Control": "no-cache",
16
+ },
17
+ }, (res) => {
18
+ if (res.statusCode !== 200) {
19
+ res.resume();
20
+ onError(new Error(`SSE connect failed: ${res.statusCode}`));
21
+ scheduleReconnect();
22
+ return;
23
+ }
24
+ res.setEncoding("utf-8");
25
+ let buffer = "";
26
+ let currentEvent = { type: "message", data: "" };
27
+ res.on("data", (chunk) => {
28
+ buffer += chunk;
29
+ const lines = buffer.split("\n");
30
+ buffer = lines.pop() ?? "";
31
+ for (const line of lines) {
32
+ if (line.startsWith("event: ")) {
33
+ currentEvent.type = line.slice(7);
34
+ }
35
+ else if (line.startsWith("data: ")) {
36
+ currentEvent.data = line.slice(6);
37
+ }
38
+ else if (line.startsWith("id: ")) {
39
+ currentEvent.id = line.slice(4);
40
+ }
41
+ else if (line === "") {
42
+ if (currentEvent.data || currentEvent.type === "heartbeat") {
43
+ onEvent({ ...currentEvent });
40
44
  }
45
+ currentEvent = { type: "message", data: "" };
41
46
  }
42
47
  }
48
+ });
49
+ res.on("end", () => {
50
+ if (!aborted)
51
+ scheduleReconnect();
52
+ });
53
+ res.on("error", (err) => {
54
+ if (!aborted) {
55
+ onError(err);
56
+ scheduleReconnect();
57
+ }
58
+ });
59
+ });
60
+ req.on("error", (err) => {
61
+ if (!aborted) {
62
+ onError(err);
63
+ scheduleReconnect();
43
64
  }
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
- }
65
+ });
66
+ req.end();
67
+ currentReq = req;
68
+ };
69
+ const scheduleReconnect = () => {
70
+ if (aborted)
71
+ return;
72
+ setTimeout(connect, 5000);
52
73
  };
53
- run();
74
+ connect();
54
75
  return {
55
76
  close: () => {
56
77
  aborted = true;
57
- controller.abort();
78
+ currentReq?.destroy();
58
79
  },
59
80
  };
60
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentfeed",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Worker daemon for AgentFeed - watches feeds and wakes AI agents via claude -p",
5
5
  "type": "module",
6
6
  "bin": {