agentfeed 0.1.4 → 0.1.6
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 +14 -1
- package/dist/invoker.js +38 -14
- package/dist/session-store.d.ts +1 -0
- package/dist/session-store.js +4 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -18,8 +18,16 @@ const serverUrl = getRequiredEnv("AGENTFEED_URL");
|
|
|
18
18
|
const apiKey = getRequiredEnv("AGENTFEED_API_KEY");
|
|
19
19
|
const client = new AgentFeedClient(serverUrl, apiKey);
|
|
20
20
|
let isRunning = false;
|
|
21
|
+
let sseConnection = null;
|
|
21
22
|
const wakeAttempts = new Map();
|
|
22
23
|
const sessionStore = new SessionStore(process.env.AGENTFEED_SESSION_FILE);
|
|
24
|
+
function shutdown() {
|
|
25
|
+
console.log("\nShutting down...");
|
|
26
|
+
sseConnection?.close();
|
|
27
|
+
process.exit(0);
|
|
28
|
+
}
|
|
29
|
+
process.on("SIGINT", shutdown);
|
|
30
|
+
process.on("SIGTERM", shutdown);
|
|
23
31
|
async function main() {
|
|
24
32
|
console.log("AgentFeed Worker starting...");
|
|
25
33
|
// Step 0: Initialize
|
|
@@ -40,7 +48,7 @@ async function main() {
|
|
|
40
48
|
// Step 2: Connect to global SSE stream
|
|
41
49
|
const sseUrl = `${serverUrl}/api/events/stream?author_type=human`;
|
|
42
50
|
console.log("Connecting to global event stream...");
|
|
43
|
-
connectSSE(sseUrl, apiKey, (rawEvent) => {
|
|
51
|
+
sseConnection = connectSSE(sseUrl, apiKey, (rawEvent) => {
|
|
44
52
|
if (rawEvent.type === "heartbeat")
|
|
45
53
|
return;
|
|
46
54
|
try {
|
|
@@ -98,6 +106,11 @@ async function handleTriggers(triggers, agent, skillMd) {
|
|
|
98
106
|
success = true;
|
|
99
107
|
break;
|
|
100
108
|
}
|
|
109
|
+
// If resume failed (stale session), clear it and retry as new session
|
|
110
|
+
if (result.exitCode !== 0 && sessionStore.get(trigger.postId)) {
|
|
111
|
+
console.log("Session may be stale, clearing and retrying as new session...");
|
|
112
|
+
sessionStore.delete(trigger.postId);
|
|
113
|
+
}
|
|
101
114
|
console.error(`Agent exited with code ${result.exitCode}, retry ${retries + 1}/${MAX_CRASH_RETRIES}`);
|
|
102
115
|
}
|
|
103
116
|
catch (err) {
|
package/dist/invoker.js
CHANGED
|
@@ -3,12 +3,16 @@ export function invokeAgent(options) {
|
|
|
3
3
|
return new Promise((resolve, reject) => {
|
|
4
4
|
const prompt = buildPrompt(options);
|
|
5
5
|
const isNewSession = !options.sessionId;
|
|
6
|
-
const args = [
|
|
6
|
+
const args = [
|
|
7
|
+
"-p", prompt,
|
|
8
|
+
"--append-system-prompt", options.skillMd,
|
|
9
|
+
"--allowedTools", "Bash",
|
|
10
|
+
];
|
|
7
11
|
if (options.sessionId) {
|
|
8
12
|
args.push("--resume", options.sessionId);
|
|
9
13
|
}
|
|
10
14
|
if (isNewSession) {
|
|
11
|
-
args.push("--output-format", "json");
|
|
15
|
+
args.push("--output-format", "stream-json", "--verbose");
|
|
12
16
|
}
|
|
13
17
|
const env = {
|
|
14
18
|
...process.env,
|
|
@@ -16,14 +20,41 @@ export function invokeAgent(options) {
|
|
|
16
20
|
AGENTFEED_API_KEY: options.apiKey,
|
|
17
21
|
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: process.env.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE ?? "50",
|
|
18
22
|
};
|
|
23
|
+
console.log("Invoking claude...");
|
|
19
24
|
const child = spawn("claude", args, {
|
|
20
25
|
env,
|
|
21
26
|
stdio: isNewSession ? ["inherit", "pipe", "inherit"] : "inherit",
|
|
22
27
|
});
|
|
23
|
-
let
|
|
28
|
+
let sessionId;
|
|
24
29
|
if (isNewSession && child.stdout) {
|
|
30
|
+
let buffer = "";
|
|
25
31
|
child.stdout.on("data", (chunk) => {
|
|
26
|
-
|
|
32
|
+
buffer += chunk.toString();
|
|
33
|
+
const lines = buffer.split("\n");
|
|
34
|
+
buffer = lines.pop() ?? "";
|
|
35
|
+
for (const line of lines) {
|
|
36
|
+
if (!line.trim())
|
|
37
|
+
continue;
|
|
38
|
+
try {
|
|
39
|
+
const event = JSON.parse(line);
|
|
40
|
+
// Show assistant text as it streams
|
|
41
|
+
if (event.type === "assistant" &&
|
|
42
|
+
event.message?.content) {
|
|
43
|
+
for (const block of event.message.content) {
|
|
44
|
+
if (block.type === "text") {
|
|
45
|
+
process.stdout.write(block.text);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Capture session_id from result event
|
|
50
|
+
if (event.type === "result" && event.session_id) {
|
|
51
|
+
sessionId = event.session_id;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// Not valid JSON, skip
|
|
56
|
+
}
|
|
57
|
+
}
|
|
27
58
|
});
|
|
28
59
|
}
|
|
29
60
|
child.on("error", (err) => {
|
|
@@ -35,16 +66,9 @@ export function invokeAgent(options) {
|
|
|
35
66
|
}
|
|
36
67
|
});
|
|
37
68
|
child.on("close", (code) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const parsed = JSON.parse(stdout);
|
|
42
|
-
sessionId = parsed.session_id;
|
|
43
|
-
}
|
|
44
|
-
catch {
|
|
45
|
-
// JSON parse failed, ignore
|
|
46
|
-
}
|
|
47
|
-
}
|
|
69
|
+
if (isNewSession)
|
|
70
|
+
process.stdout.write("\n");
|
|
71
|
+
console.log(`Agent exited (code ${code ?? "unknown"})`);
|
|
48
72
|
resolve({ exitCode: code ?? 1, sessionId: sessionId ?? options.sessionId });
|
|
49
73
|
});
|
|
50
74
|
});
|
package/dist/session-store.d.ts
CHANGED
package/dist/session-store.js
CHANGED