agentfeed 0.1.0 → 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.
- package/dist/index.js +5 -0
- package/dist/invoker.d.ts +5 -2
- package/dist/invoker.js +34 -13
- package/dist/sse-client.js +68 -47
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -18,6 +18,7 @@ const apiKey = getRequiredEnv("AGENTFEED_API_KEY");
|
|
|
18
18
|
const client = new AgentFeedClient(serverUrl, apiKey);
|
|
19
19
|
let isRunning = false;
|
|
20
20
|
const wakeAttempts = new Map();
|
|
21
|
+
const sessionMap = new Map(); // postId → claude sessionId
|
|
21
22
|
async function main() {
|
|
22
23
|
console.log("AgentFeed Worker starting...");
|
|
23
24
|
// Step 0: Initialize
|
|
@@ -87,7 +88,11 @@ async function handleTriggers(triggers, agent, skillMd) {
|
|
|
87
88
|
apiKey,
|
|
88
89
|
serverUrl,
|
|
89
90
|
recentContext,
|
|
91
|
+
sessionId: sessionMap.get(trigger.postId),
|
|
90
92
|
});
|
|
93
|
+
if (result.sessionId) {
|
|
94
|
+
sessionMap.set(trigger.postId, result.sessionId);
|
|
95
|
+
}
|
|
91
96
|
if (result.exitCode === 0) {
|
|
92
97
|
success = true;
|
|
93
98
|
break;
|
package/dist/invoker.d.ts
CHANGED
|
@@ -6,8 +6,11 @@ interface InvokeOptions {
|
|
|
6
6
|
apiKey: string;
|
|
7
7
|
serverUrl: string;
|
|
8
8
|
recentContext: string;
|
|
9
|
+
sessionId?: string;
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
+
interface InvokeResult {
|
|
11
12
|
exitCode: number;
|
|
12
|
-
|
|
13
|
+
sessionId?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function invokeAgent(options: InvokeOptions): Promise<InvokeResult>;
|
|
13
16
|
export {};
|
package/dist/invoker.js
CHANGED
|
@@ -2,19 +2,30 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
export function invokeAgent(options) {
|
|
3
3
|
return new Promise((resolve, reject) => {
|
|
4
4
|
const prompt = buildPrompt(options);
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"--
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
5
|
+
const isNewSession = !options.sessionId;
|
|
6
|
+
const args = ["-p", prompt, "--append-system-prompt", options.skillMd];
|
|
7
|
+
if (options.sessionId) {
|
|
8
|
+
args.push("--resume", options.sessionId);
|
|
9
|
+
}
|
|
10
|
+
if (isNewSession) {
|
|
11
|
+
args.push("--output-format", "json");
|
|
12
|
+
}
|
|
13
|
+
const env = {
|
|
14
|
+
...process.env,
|
|
15
|
+
AGENTFEED_BASE_URL: `${options.serverUrl}/api`,
|
|
16
|
+
AGENTFEED_API_KEY: options.apiKey,
|
|
17
|
+
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: process.env.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE ?? "50",
|
|
18
|
+
};
|
|
19
|
+
const child = spawn("claude", args, {
|
|
20
|
+
env,
|
|
21
|
+
stdio: isNewSession ? ["inherit", "pipe", "inherit"] : "inherit",
|
|
17
22
|
});
|
|
23
|
+
let stdout = "";
|
|
24
|
+
if (isNewSession && child.stdout) {
|
|
25
|
+
child.stdout.on("data", (chunk) => {
|
|
26
|
+
stdout += chunk.toString();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
18
29
|
child.on("error", (err) => {
|
|
19
30
|
if (err.code === "ENOENT") {
|
|
20
31
|
reject(new Error("'claude' command not found. Install Claude Code: https://claude.ai/claude-code"));
|
|
@@ -24,7 +35,17 @@ export function invokeAgent(options) {
|
|
|
24
35
|
}
|
|
25
36
|
});
|
|
26
37
|
child.on("close", (code) => {
|
|
27
|
-
|
|
38
|
+
let sessionId;
|
|
39
|
+
if (isNewSession && stdout) {
|
|
40
|
+
try {
|
|
41
|
+
const parsed = JSON.parse(stdout);
|
|
42
|
+
sessionId = parsed.session_id;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// JSON parse failed, ignore
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
resolve({ exitCode: code ?? 1, sessionId: sessionId ?? options.sessionId });
|
|
28
49
|
});
|
|
29
50
|
});
|
|
30
51
|
}
|
package/dist/sse-client.js
CHANGED
|
@@ -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
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
74
|
+
connect();
|
|
54
75
|
return {
|
|
55
76
|
close: () => {
|
|
56
77
|
aborted = true;
|
|
57
|
-
|
|
78
|
+
currentReq?.destroy();
|
|
58
79
|
},
|
|
59
80
|
};
|
|
60
81
|
}
|