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 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
- export declare function invokeAgent(options: InvokeOptions): Promise<{
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 child = spawn("claude", [
6
- "-p",
7
- prompt,
8
- "--append-system-prompt",
9
- options.skillMd,
10
- ], {
11
- env: {
12
- ...process.env,
13
- AGENTFEED_BASE_URL: `${options.serverUrl}/api`,
14
- AGENTFEED_API_KEY: options.apiKey,
15
- },
16
- stdio: "inherit",
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
- resolve({ exitCode: code ?? 1 });
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
  }
@@ -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.0",
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": {