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 CHANGED
@@ -2,31 +2,13 @@
2
2
 
3
3
  # neckbeard
4
4
 
5
- There's a weird thing that happens when you try to deploy an AI agent.
5
+ Neckbeard deploys your agent code into an E2B or Daytona sandbox and runs it behind a small persistent HTTP server inside that sandbox.
6
6
 
7
- Most people think of agents as fancy API calls. You send a prompt, you get a response. But that's not what's actually happening. The agent is running code. It's executing bash commands, writing files, installing packages. It runs for minutes at a time, maintaining state between steps. It's a process, not a request.
8
-
9
- This creates an obvious problem: do you really want that process running on your production server?
10
-
11
- Anthropic's answer is no. Their [hosting docs](https://docs.claude.com/en/docs/agent-sdk/hosting) say you should run the entire agent inside a sandbox. Not just intercept the dangerous tool calls—put the whole thing in a container where it can't escape.
12
-
13
- That sounds simple. It's not.
14
-
15
- ## The Plumbing Problem
16
-
17
- Sandboxes like E2B give you a fresh Linux container. Your agent code lives in your repo. Bridging these two worlds is surprisingly annoying.
18
-
19
- First, you have to get your code into the sandbox. You could bake it into a custom template, but then you're rebuilding templates every time you change a line. You could git clone on boot, but that's slow and requires auth. You could bundle and upload at runtime, which works, but now you're writing bundler configs.
20
-
21
- Then you have to pass input. How do you get the user's prompt into a process running inside a sandbox? CLI arguments require escaping and have length limits. Environment variables have size limits. Writing to a file works, but adds boilerplate.
22
-
23
- The worst part is output. When you run `sandbox.exec("node agent.js")`, you get back everything the process printed. SDK logs, debug output, streaming tokens, and somewhere in there, your actual result. Good luck parsing that reliably.
24
-
25
- So you end up writing results to a file, reading it back, parsing JSON, validating the shape, handling errors. Every team building sandboxed agents writes some version of this plumbing. It's tedious.
7
+ The intent is simple: your production process should not run the agent. The sandbox runs the agent, keeps its filesystem/process state, and exposes a private `/invoke` endpoint that neckbeard calls for each turn.
26
8
 
27
9
  ## What This Does
28
10
 
29
- Neckbeard handles all of that so you can just write your agent:
11
+ You write an agent:
30
12
 
31
13
  ```typescript
32
14
  import { Agent } from 'agent-neckbeard';
@@ -56,14 +38,17 @@ const agent = new Agent({
56
38
  return JSON.parse(message.result ?? '{}');
57
39
  }
58
40
  }
41
+ throw new Error('Claude did not return a result');
59
42
  },
60
43
  });
61
44
 
62
- const sandboxId = await agent.deploy(); // bundles, uploads, returns sandboxId
63
- const result = await agent.run({ topic: 'TypeScript generics' });
45
+ const sandboxId = await agent.deploy();
46
+ const result = await agent.run({ topic: 'TypeScript generics' }, { sandboxId });
64
47
  ```
65
48
 
66
- `deploy()` bundles your code with esbuild and uploads it. `run()` writes input to a file, executes, reads the result back, and validates it against your schema. You don't think about file paths or stdout parsing.
49
+ `deploy()` bundles your agent, uploads `agent.mjs` and a TypeScript runtime server to the sandbox, starts `node server.mjs`, waits for `/health`, and returns the sandbox ID.
50
+
51
+ `run()` validates input locally, POSTs it to the sandbox server, reads SSE harness events, validates the final output, and returns the existing `{ ok, executionId, output | error }` result shape.
67
52
 
68
53
  ## Setup
69
54
 
@@ -74,48 +59,122 @@ npm install agent-neckbeard
74
59
  ```bash
75
60
  export E2B_API_KEY=your-key
76
61
  export DAYTONA_API_KEY=your-key
77
- export ANTHROPIC_API_KEY=your-key # Pass via envs config, not auto-forwarded
62
+ export ANTHROPIC_API_KEY=your-key
78
63
  ```
79
64
 
80
- Pick the sandbox backend explicitly with `sandbox.provider`. Use the same public `sandbox.template` field for both providers: it maps to an E2B template or a Daytona snapshot.
65
+ Pick the sandbox backend explicitly with `sandbox.provider`. The public `sandbox.template` field maps to an E2B template or a Daytona snapshot.
81
66
 
82
- ## The Details
67
+ ## Run-Output Agents
83
68
 
84
- The constructor takes a few options:
69
+ Use `run` when each invocation should produce one validated output:
85
70
 
86
71
  ```typescript
87
72
  new Agent({
88
73
  sandbox: {
89
- provider: 'e2b' | 'daytona',
90
- template: string, // E2B template or Daytona snapshot
74
+ provider: 'e2b',
75
+ template: 'code-interpreter-v1',
91
76
  },
92
- inputSchema: ZodSchema,
93
- outputSchema: ZodSchema,
94
- run: (input, ctx) => Promise,
95
- maxDuration?: number, // seconds, default 300
96
- dependencies?: {
97
- apt?: string[],
98
- commands?: string[],
77
+ inputSchema: z.object({ prompt: z.string() }),
78
+ outputSchema: z.object({ answer: z.string() }),
79
+ run: async (input, ctx) => {
80
+ ctx.logger.info(`Handling ${ctx.executionId}`);
81
+ return { answer: input.prompt.toUpperCase() };
82
+ },
83
+ });
84
+ ```
85
+
86
+ The `run` handler receives a context object with `executionId`, `AbortSignal`, environment variables, sandbox paths, and a logger:
87
+
88
+ ```typescript
89
+ run: async (input, ctx) => {
90
+ const notesPath = `${ctx.sandbox.homeDir}/data/notes.txt`;
91
+ const cwd = ctx.sandbox.agentDir;
92
+ // ...
93
+ }
94
+ ```
95
+
96
+ Use `ctx.sandbox.homeDir` and `ctx.sandbox.agentDir` instead of hardcoding `/home/user` if you want the same agent code to work on both providers.
97
+
98
+ ## Streaming Agents
99
+
100
+ Use `server.handler` when you want progress frames while the sandbox work is still running:
101
+
102
+ ```typescript
103
+ const agent = new Agent({
104
+ sandbox: {
105
+ provider: 'daytona',
106
+ template: 'daytona-small',
99
107
  },
100
- files?: [{ url, path }], // pre-download into sandbox
101
- claudeDir?: string, // upload .claude/ skills directory
102
- envs?: Record<string, string | undefined>, // environment variables for sandbox
103
- })
108
+ inputSchema: z.object({ prompt: z.string() }),
109
+ server: {
110
+ frameSchema: z.object({
111
+ type: z.enum(['status', 'token', 'done']),
112
+ text: z.string().optional(),
113
+ }),
114
+ handler: async ({ input, emit }) => {
115
+ await emit({ type: 'status', text: `Starting ${input.prompt}` });
116
+ await emit({ type: 'token', text: 'hello' });
117
+ await emit({ type: 'done' });
118
+ },
119
+ },
120
+ });
121
+
122
+ const sandboxId = await agent.deploy();
123
+
124
+ for await (const frame of agent.invoke({ prompt: 'hi' }, { sandboxId })) {
125
+ console.log(frame);
126
+ }
104
127
  ```
105
128
 
106
- `deploy()` returns the sandbox ID, which you can save for reconnecting later:
129
+ Internally, streaming uses Server-Sent Events from the sandbox `/invoke` endpoint. Neckbeard wraps user frames in harness events so errors, completion, and run-output results are handled consistently.
130
+
131
+ ## Reusing Sandboxes
132
+
133
+ Persist the sandbox ID if another process needs to call the same running sandbox server:
107
134
 
108
135
  ```typescript
109
136
  const sandboxId = await agent.deploy();
110
- // Later, reconnect to the same sandbox:
111
- const result = await agent.run({ topic: 'hello' }, { sandboxId });
137
+ await saveSandboxId(sandboxId);
138
+
139
+ // Later:
140
+ const savedSandboxId = await loadSandboxId();
141
+ const result = await agent.run({ topic: 'hello' }, { sandboxId: savedSandboxId });
112
142
  ```
113
143
 
114
- The `files` option downloads things into the sandbox before your agent runs—useful for models or config files. Relative paths resolve from the sandbox home directory: `/home/user` on E2B and `/home/daytona` on Daytona.
144
+ Neckbeard rehydrates the sandbox endpoint and provider traffic token from the sandbox ID using the configured provider SDK. E2B traffic tokens are sent as the `e2b-traffic-access-token` header. Daytona preview tokens are sent as the `DAYTONA_SANDBOX_AUTH_KEY` query parameter. Consumers only need to store the sandbox ID and configure the same provider/API key in the process that reconnects.
145
+
146
+ ## Configuration
147
+
148
+ ```typescript
149
+ new Agent({
150
+ sandbox: {
151
+ provider: 'e2b' | 'daytona',
152
+ template: string,
153
+ },
154
+ inputSchema: ZodSchema,
155
+ outputSchema?: ZodSchema,
156
+ run?: (input, ctx) => Promise<output>,
157
+ server?: {
158
+ port?: number,
159
+ frameSchema: ZodSchema,
160
+ handler: ({ input, ctx, signal, emit }) => Promise<void>,
161
+ },
162
+ maxDuration?: number,
163
+ dependencies?: {
164
+ apt?: string[],
165
+ commands?: string[],
166
+ },
167
+ files?: [{ url, path }],
168
+ claudeDir?: string,
169
+ envs?: Record<string, string | undefined>,
170
+ });
171
+ ```
172
+
173
+ The `files` option downloads files into the sandbox before the server starts. Relative paths resolve from the sandbox home directory: `/home/user` on E2B and `/home/daytona` on Daytona.
115
174
 
116
175
  The `claudeDir` option uploads a local `.claude/` directory to the sandbox, enabling Claude Agent SDK skills. Point it at a directory containing `.claude/skills/*/SKILL.md` files.
117
176
 
118
- The `envs` option passes environment variables to the sandbox. These are available to your agent code via `process.env` and `ctx.env`. Undefined values are filtered out:
177
+ The `envs` option passes environment variables to the sandbox. Undefined values are filtered out:
119
178
 
120
179
  ```typescript
121
180
  const agent = new Agent({
@@ -131,19 +190,21 @@ const agent = new Agent({
131
190
  });
132
191
  ```
133
192
 
134
- Some packages can't be bundled because they spawn child processes or have native modules. The Claude Agent SDK is like this. These get automatically marked as external and installed via npm in the sandbox.
193
+ Some packages cannot be bundled because they spawn child processes or have native modules. The Claude Agent SDK is like this. These packages are automatically marked external and installed with npm in the sandbox.
135
194
 
136
- The `run` function gets a context object with an `executionId`, an `AbortSignal`, environment variables, sandbox paths, and a logger:
195
+ ## Cleanup
137
196
 
138
197
  ```typescript
139
- run: async (input, ctx) => {
140
- const notesPath = `${ctx.sandbox.homeDir}/data/notes.txt`;
141
- const cwd = ctx.sandbox.agentDir;
142
- // ...
198
+ const sandboxId = await agent.deploy();
199
+
200
+ try {
201
+ const result = await agent.run(input, { sandboxId });
202
+ } finally {
203
+ await agent.kill(sandboxId);
143
204
  }
144
205
  ```
145
206
 
146
- Use `ctx.sandbox.homeDir` and `ctx.sandbox.agentDir` instead of hardcoding `/home/user` if you want the same agent code to work on both providers.
207
+ `kill()` accepts a sandbox ID. If omitted, it kills the sandbox cached on the agent instance.
147
208
 
148
209
  ## Testing
149
210