agent-neckbeard 1.1.6 → 1.1.8
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/README.md +114 -53
- package/dist/index.cjs +360 -475
- package/dist/index.d.cts +50 -74
- package/dist/index.d.ts +50 -74
- package/dist/index.js +310 -465
- package/dist/{invoke-client-Y3NPRFEE.js → invoke-client-WYMFPR3F.js} +45 -11
- package/dist/runtime/server.js +163 -0
- package/package.json +3 -3
|
@@ -37,17 +37,45 @@ function parseFrame(raw) {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
// src/runtime/protocol.ts
|
|
41
|
+
function isHarnessEvent(value) {
|
|
42
|
+
if (!value || typeof value !== "object") return false;
|
|
43
|
+
const candidate = value;
|
|
44
|
+
return typeof candidate.kind === "string" && typeof candidate.invocationId === "string" && ["started", "frame", "output", "done", "error"].includes(candidate.kind);
|
|
45
|
+
}
|
|
46
|
+
|
|
40
47
|
// src/invoke-client.ts
|
|
48
|
+
function createInvocationId() {
|
|
49
|
+
return `invoke_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
50
|
+
}
|
|
51
|
+
function appendDaytonaTrafficToken(url, token) {
|
|
52
|
+
if (!token) return url;
|
|
53
|
+
const separator = url.includes("?") ? "&" : "?";
|
|
54
|
+
return `${url}${separator}DAYTONA_SANDBOX_AUTH_KEY=${encodeURIComponent(token)}`;
|
|
55
|
+
}
|
|
56
|
+
function buildRequestUrl(options) {
|
|
57
|
+
if (options.provider === "daytona") {
|
|
58
|
+
return appendDaytonaTrafficToken(options.serverUrl, options.trafficToken);
|
|
59
|
+
}
|
|
60
|
+
return options.serverUrl;
|
|
61
|
+
}
|
|
62
|
+
function buildRequestHeaders(options) {
|
|
63
|
+
const headers = {
|
|
64
|
+
"content-type": "application/json",
|
|
65
|
+
accept: "text/event-stream"
|
|
66
|
+
};
|
|
67
|
+
if (options.provider === "e2b" && options.trafficToken) {
|
|
68
|
+
headers["e2b-traffic-access-token"] = options.trafficToken;
|
|
69
|
+
}
|
|
70
|
+
return headers;
|
|
71
|
+
}
|
|
41
72
|
async function* invokeServer(input, options) {
|
|
42
73
|
const fetchImpl = options.fetch ?? fetch;
|
|
43
|
-
const
|
|
74
|
+
const invocationId = options.invocationId ?? createInvocationId();
|
|
75
|
+
const response = await fetchImpl(buildRequestUrl(options), {
|
|
44
76
|
method: "POST",
|
|
45
|
-
headers:
|
|
46
|
-
|
|
47
|
-
accept: "text/event-stream",
|
|
48
|
-
authorization: `Bearer ${options.authToken}`
|
|
49
|
-
},
|
|
50
|
-
body: JSON.stringify(input),
|
|
77
|
+
headers: buildRequestHeaders(options),
|
|
78
|
+
body: JSON.stringify({ invocationId, input }),
|
|
51
79
|
signal: options.signal
|
|
52
80
|
});
|
|
53
81
|
if (!response.ok) {
|
|
@@ -75,11 +103,17 @@ async function* invokeServer(input, options) {
|
|
|
75
103
|
}
|
|
76
104
|
}
|
|
77
105
|
}
|
|
78
|
-
for await (const
|
|
79
|
-
if (
|
|
80
|
-
|
|
106
|
+
for await (const rawEvent of decodeSseStream(chunks())) {
|
|
107
|
+
if (!isHarnessEvent(rawEvent)) {
|
|
108
|
+
throw new Error("Server returned an invalid harness event");
|
|
109
|
+
}
|
|
110
|
+
if (rawEvent.kind === "frame" && options.frameSchema) {
|
|
111
|
+
yield {
|
|
112
|
+
...rawEvent,
|
|
113
|
+
data: options.frameSchema.parse(rawEvent.data)
|
|
114
|
+
};
|
|
81
115
|
} else {
|
|
82
|
-
yield
|
|
116
|
+
yield rawEvent;
|
|
83
117
|
}
|
|
84
118
|
}
|
|
85
119
|
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// src/runtime/server.ts
|
|
2
|
+
import { createServer } from "http";
|
|
3
|
+
import { readFileSync } from "fs";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import { dirname, join } from "path";
|
|
6
|
+
var CONFIG_PATH = join(dirname(fileURLToPath(import.meta.url)), "server-config.json");
|
|
7
|
+
var CONFIG = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
|
|
8
|
+
function createLogger() {
|
|
9
|
+
return {
|
|
10
|
+
debug: (message, ...args) => console.log("[DEBUG]", message, ...args),
|
|
11
|
+
info: (message, ...args) => console.log("[INFO]", message, ...args),
|
|
12
|
+
warn: (message, ...args) => console.warn("[WARN]", message, ...args),
|
|
13
|
+
error: (message, ...args) => console.error("[ERROR]", message, ...args)
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function serializeError(error) {
|
|
17
|
+
if (error instanceof Error) {
|
|
18
|
+
return {
|
|
19
|
+
name: error.name,
|
|
20
|
+
message: error.message,
|
|
21
|
+
stack: error.stack
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return { message: String(error) };
|
|
25
|
+
}
|
|
26
|
+
function sendJson(res, status, body) {
|
|
27
|
+
res.writeHead(status, { "content-type": "application/json" });
|
|
28
|
+
res.end(JSON.stringify(body));
|
|
29
|
+
}
|
|
30
|
+
function sendSseFrame(res, event) {
|
|
31
|
+
res.write(`data: ${JSON.stringify(event)}
|
|
32
|
+
|
|
33
|
+
`);
|
|
34
|
+
}
|
|
35
|
+
async function readRequestBody(req) {
|
|
36
|
+
const chunks = [];
|
|
37
|
+
for await (const chunk of req) {
|
|
38
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
39
|
+
}
|
|
40
|
+
return Buffer.concat(chunks).toString("utf-8");
|
|
41
|
+
}
|
|
42
|
+
async function loadAgent() {
|
|
43
|
+
const mod = await import(new URL("./agent.mjs", import.meta.url).href);
|
|
44
|
+
const agent = (mod.default && (mod.default._run || mod.default._server) ? mod.default : null) || Object.values(mod).find(
|
|
45
|
+
(value) => value && typeof value === "object" && ("_run" in value || "_server" in value)
|
|
46
|
+
);
|
|
47
|
+
if (!agent || typeof agent !== "object") {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`No agent found in agent.mjs. Exports: ${Object.keys(mod).join(", ")}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return agent;
|
|
53
|
+
}
|
|
54
|
+
async function main() {
|
|
55
|
+
const agent = await loadAgent();
|
|
56
|
+
const server = createServer(async (req, res) => {
|
|
57
|
+
try {
|
|
58
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
59
|
+
if (url.pathname === "/health" && req.method === "GET") {
|
|
60
|
+
return sendJson(res, 200, { ok: true });
|
|
61
|
+
}
|
|
62
|
+
if (url.pathname === "/shutdown" && req.method === "POST") {
|
|
63
|
+
sendJson(res, 200, { ok: true });
|
|
64
|
+
setTimeout(() => shutdown(), 10).unref();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (url.pathname === "/invoke" && req.method === "POST") {
|
|
68
|
+
const rawBody = await readRequestBody(req);
|
|
69
|
+
let requestBody;
|
|
70
|
+
try {
|
|
71
|
+
requestBody = JSON.parse(rawBody);
|
|
72
|
+
} catch {
|
|
73
|
+
return sendJson(res, 400, { error: "invalid JSON body" });
|
|
74
|
+
}
|
|
75
|
+
if (!requestBody || typeof requestBody !== "object" || typeof requestBody.invocationId !== "string") {
|
|
76
|
+
return sendJson(res, 400, { error: "invalid invoke request" });
|
|
77
|
+
}
|
|
78
|
+
let validatedInput;
|
|
79
|
+
try {
|
|
80
|
+
validatedInput = agent.inputSchema.parse(requestBody.input);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return sendJson(res, 400, {
|
|
83
|
+
error: `input validation failed: ${serializeError(error).message}`
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
res.writeHead(200, {
|
|
87
|
+
"content-type": "text/event-stream",
|
|
88
|
+
"cache-control": "no-cache, no-transform",
|
|
89
|
+
connection: "keep-alive",
|
|
90
|
+
"x-accel-buffering": "no"
|
|
91
|
+
});
|
|
92
|
+
const controller = new AbortController();
|
|
93
|
+
req.on("aborted", () => controller.abort());
|
|
94
|
+
res.on("close", () => {
|
|
95
|
+
if (!res.writableEnded) {
|
|
96
|
+
controller.abort();
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
const timeout = setTimeout(() => controller.abort(), CONFIG.maxDurationMs);
|
|
100
|
+
const invocationId = requestBody.invocationId;
|
|
101
|
+
const emitHarnessEvent = (event) => {
|
|
102
|
+
if (!res.writableEnded) {
|
|
103
|
+
sendSseFrame(res, event);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const ctx = {
|
|
107
|
+
executionId: invocationId,
|
|
108
|
+
signal: controller.signal,
|
|
109
|
+
env: process.env,
|
|
110
|
+
sandbox: CONFIG.sandboxContext,
|
|
111
|
+
logger: createLogger()
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
emitHarnessEvent({ kind: "started", invocationId });
|
|
115
|
+
if (agent._server) {
|
|
116
|
+
await agent._server.handler({
|
|
117
|
+
input: validatedInput,
|
|
118
|
+
ctx,
|
|
119
|
+
signal: controller.signal,
|
|
120
|
+
emit: async (data) => emitHarnessEvent({ kind: "frame", invocationId, data })
|
|
121
|
+
});
|
|
122
|
+
} else if (agent._run) {
|
|
123
|
+
const output = await agent._run(validatedInput, ctx);
|
|
124
|
+
emitHarnessEvent({ kind: "output", invocationId, output });
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error("Agent has neither a run handler nor a server handler.");
|
|
127
|
+
}
|
|
128
|
+
emitHarnessEvent({ kind: "done", invocationId });
|
|
129
|
+
} catch (error) {
|
|
130
|
+
emitHarnessEvent({ kind: "error", invocationId, error: serializeError(error) });
|
|
131
|
+
} finally {
|
|
132
|
+
clearTimeout(timeout);
|
|
133
|
+
if (!res.writableEnded) {
|
|
134
|
+
res.end();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
return sendJson(res, 404, { error: "not found" });
|
|
140
|
+
} catch (error) {
|
|
141
|
+
console.error("[neckbeard-server] unexpected error", error);
|
|
142
|
+
if (!res.headersSent) {
|
|
143
|
+
sendJson(res, 500, { error: "internal error" });
|
|
144
|
+
} else if (!res.writableEnded) {
|
|
145
|
+
res.end();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
function shutdown() {
|
|
150
|
+
console.log("[neckbeard-server] shutting down");
|
|
151
|
+
server.close(() => process.exit(0));
|
|
152
|
+
setTimeout(() => process.exit(0), 5e3).unref();
|
|
153
|
+
}
|
|
154
|
+
server.listen(CONFIG.port, "0.0.0.0", () => {
|
|
155
|
+
console.log(`[neckbeard-server] listening on port ${CONFIG.port}`);
|
|
156
|
+
});
|
|
157
|
+
process.on("SIGTERM", shutdown);
|
|
158
|
+
process.on("SIGINT", shutdown);
|
|
159
|
+
}
|
|
160
|
+
main().catch((error) => {
|
|
161
|
+
console.error("[neckbeard-server] failed to start", error);
|
|
162
|
+
process.exit(1);
|
|
163
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agent-neckbeard",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"description": "Deploy AI agents to E2B and Daytona sandboxes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"node": ">=20.0.0"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
|
-
"build": "tsup",
|
|
29
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsup",
|
|
30
30
|
"dev": "tsup --watch",
|
|
31
31
|
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --config jest.config.mjs",
|
|
32
32
|
"test:watch": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --config jest.config.mjs --watch",
|
|
33
33
|
"test:e2e": "npm run build && node --experimental-vm-modules ./node_modules/jest/bin/jest.js --config jest.e2e.config.mjs --runInBand",
|
|
34
34
|
"test:e2e:e2b": "npm run build && node --experimental-vm-modules ./node_modules/jest/bin/jest.js --config jest.e2e.config.mjs --runInBand --testNamePattern=\"live E2B smoke tests\"",
|
|
35
35
|
"test:e2e:daytona": "npm run build && node --experimental-vm-modules ./node_modules/jest/bin/jest.js --config jest.e2e.config.mjs --runInBand --testNamePattern=\"live Daytona smoke tests\"",
|
|
36
|
-
"test:cjs": "npm run build && node --experimental-vm-modules ./node_modules/jest/bin/jest.js --config jest.config.mjs --
|
|
36
|
+
"test:cjs": "npm run build && node --experimental-vm-modules ./node_modules/jest/bin/jest.js --config jest.config.mjs --testPathPatterns=cjs-build",
|
|
37
37
|
"typecheck": "tsc --noEmit -p tsconfig.test.json",
|
|
38
38
|
"prepare": "npm run build"
|
|
39
39
|
},
|