agentfeed 0.1.2 → 0.1.4
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/dist/index.js +4 -3
- package/dist/session-store.d.ts +9 -0
- package/dist/session-store.js +41 -0
- package/dist/sse-client.js +25 -75
- package/package.json +4 -1
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { connectSSE } from "./sse-client.js";
|
|
|
3
3
|
import { detectTrigger } from "./trigger.js";
|
|
4
4
|
import { invokeAgent } from "./invoker.js";
|
|
5
5
|
import { scanUnprocessed } from "./scanner.js";
|
|
6
|
+
import { SessionStore } from "./session-store.js";
|
|
6
7
|
const MAX_WAKE_ATTEMPTS = 3;
|
|
7
8
|
const MAX_CRASH_RETRIES = 3;
|
|
8
9
|
function getRequiredEnv(name) {
|
|
@@ -18,7 +19,7 @@ const apiKey = getRequiredEnv("AGENTFEED_API_KEY");
|
|
|
18
19
|
const client = new AgentFeedClient(serverUrl, apiKey);
|
|
19
20
|
let isRunning = false;
|
|
20
21
|
const wakeAttempts = new Map();
|
|
21
|
-
const
|
|
22
|
+
const sessionStore = new SessionStore(process.env.AGENTFEED_SESSION_FILE);
|
|
22
23
|
async function main() {
|
|
23
24
|
console.log("AgentFeed Worker starting...");
|
|
24
25
|
// Step 0: Initialize
|
|
@@ -88,10 +89,10 @@ async function handleTriggers(triggers, agent, skillMd) {
|
|
|
88
89
|
apiKey,
|
|
89
90
|
serverUrl,
|
|
90
91
|
recentContext,
|
|
91
|
-
sessionId:
|
|
92
|
+
sessionId: sessionStore.get(trigger.postId),
|
|
92
93
|
});
|
|
93
94
|
if (result.sessionId) {
|
|
94
|
-
|
|
95
|
+
sessionStore.set(trigger.postId, result.sessionId);
|
|
95
96
|
}
|
|
96
97
|
if (result.exitCode === 0) {
|
|
97
98
|
success = true;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
const DEFAULT_DIR = join(homedir(), ".agentfeed");
|
|
5
|
+
export class SessionStore {
|
|
6
|
+
map = new Map();
|
|
7
|
+
filePath;
|
|
8
|
+
constructor(filePath) {
|
|
9
|
+
this.filePath = filePath ?? join(DEFAULT_DIR, "sessions.json");
|
|
10
|
+
this.load();
|
|
11
|
+
}
|
|
12
|
+
get(postId) {
|
|
13
|
+
return this.map.get(postId);
|
|
14
|
+
}
|
|
15
|
+
set(postId, sessionId) {
|
|
16
|
+
this.map.set(postId, sessionId);
|
|
17
|
+
this.save();
|
|
18
|
+
}
|
|
19
|
+
load() {
|
|
20
|
+
try {
|
|
21
|
+
const raw = readFileSync(this.filePath, "utf-8");
|
|
22
|
+
const data = JSON.parse(raw);
|
|
23
|
+
for (const [k, v] of Object.entries(data)) {
|
|
24
|
+
this.map.set(k, v);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// File doesn't exist or invalid JSON — start fresh
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
save() {
|
|
32
|
+
try {
|
|
33
|
+
mkdirSync(dirname(this.filePath), { recursive: true });
|
|
34
|
+
const data = Object.fromEntries(this.map);
|
|
35
|
+
writeFileSync(this.filePath, JSON.stringify(data, null, 2), "utf-8");
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
console.error("Failed to save sessions:", err);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
package/dist/sse-client.js
CHANGED
|
@@ -1,81 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { EventSource } from "eventsource";
|
|
2
|
+
const EVENT_TYPES = ["heartbeat", "post_created", "comment_created"];
|
|
3
3
|
export function connectSSE(url, apiKey, onEvent, onError) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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 });
|
|
44
|
-
}
|
|
45
|
-
currentEvent = { type: "message", data: "" };
|
|
46
|
-
}
|
|
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();
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
req.end();
|
|
67
|
-
currentReq = req;
|
|
4
|
+
const es = new EventSource(url, {
|
|
5
|
+
fetch: (input, init) => fetch(input, {
|
|
6
|
+
...init,
|
|
7
|
+
headers: { ...init.headers, Authorization: `Bearer ${apiKey}` },
|
|
8
|
+
}),
|
|
9
|
+
});
|
|
10
|
+
es.onopen = () => {
|
|
11
|
+
console.log("SSE connected.");
|
|
68
12
|
};
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
|
|
72
|
-
|
|
13
|
+
es.onerror = (err) => {
|
|
14
|
+
if (es.readyState === EventSource.CLOSED) {
|
|
15
|
+
onError(new Error(err.message ?? "SSE connection closed"));
|
|
16
|
+
}
|
|
17
|
+
// CONNECTING state = auto-reconnecting, no action needed
|
|
73
18
|
};
|
|
74
|
-
|
|
19
|
+
for (const eventType of EVENT_TYPES) {
|
|
20
|
+
es.addEventListener(eventType, (e) => {
|
|
21
|
+
onEvent({
|
|
22
|
+
type: eventType,
|
|
23
|
+
data: e.data,
|
|
24
|
+
id: e.lastEventId || undefined,
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
75
28
|
return {
|
|
76
|
-
close: () =>
|
|
77
|
-
aborted = true;
|
|
78
|
-
currentReq?.destroy();
|
|
79
|
-
},
|
|
29
|
+
close: () => es.close(),
|
|
80
30
|
};
|
|
81
31
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentfeed",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Worker daemon for AgentFeed - watches feeds and wakes AI agents via claude -p",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -21,5 +21,8 @@
|
|
|
21
21
|
"@types/node": "^22",
|
|
22
22
|
"tsx": "^4",
|
|
23
23
|
"typescript": "^5"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"eventsource": "^4.1.0"
|
|
24
27
|
}
|
|
25
28
|
}
|