|
| 1 | +--- |
| 2 | +title: Agent Runtime |
| 3 | +description: CLI flags, built-in agent handlers, and custom handler authoring for the runtimeuse server. |
| 4 | +--- |
| 5 | + |
| 6 | +The [agent runtime](https://www.npmjs.com/package/runtimeuse) is the process that runs inside the sandbox. It exposes a WebSocket server, receives invocations from the Python client, and delegates work to an agent handler. |
| 7 | + |
| 8 | +## CLI |
| 9 | + |
| 10 | +```bash |
| 11 | +npx -y runtimeuse # OpenAI handler on port 8080 |
| 12 | +npx -y runtimeuse --agent claude # Claude handler |
| 13 | +npx -y runtimeuse --port 3000 # custom port |
| 14 | +npx -y runtimeuse --handler ./my-handler.js # custom handler entrypoint |
| 15 | +``` |
| 16 | + |
| 17 | +## Built-in Handlers |
| 18 | + |
| 19 | +- `openai`: the default handler, uses the [OpenAI Agents SDK](https://openai.github.io/openai-agents-js/) with shell and web search tools. |
| 20 | +- `claude`: uses [Claude Agents SDK](https://platform.claude.com/docs/en/agent-sdk/overview) with Claude Code preset. |
| 21 | + |
| 22 | +### OpenAI Handler |
| 23 | + |
| 24 | +Requires `OPENAI_API_KEY` to be set in the environment. The handler runs the agent with shell access and web search enabled. |
| 25 | + |
| 26 | +```bash |
| 27 | +export OPENAI_API_KEY=your_openai_api_key |
| 28 | +npx -y runtimeuse |
| 29 | +``` |
| 30 | + |
| 31 | +### Claude Handler |
| 32 | + |
| 33 | +Requires the `@anthropic-ai/claude-code` CLI and `ANTHROPIC_API_KEY`. Always set `IS_SANDBOX=1` and `CLAUDE_SKIP_ROOT_CHECK=1` in the sandbox environment. |
| 34 | + |
| 35 | +```bash |
| 36 | +npm install -g @anthropic-ai/claude-code |
| 37 | +export ANTHROPIC_API_KEY=your_anthropic_api_key |
| 38 | +export IS_SANDBOX=1 |
| 39 | +export CLAUDE_SKIP_ROOT_CHECK=1 |
| 40 | +npx -y runtimeuse --agent claude |
| 41 | +``` |
| 42 | + |
| 43 | +## Programmatic Startup |
| 44 | + |
| 45 | +If you want to embed RuntimeUse directly in your own Node process, start it programmatically: |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { RuntimeUseServer, openaiHandler } from "runtimeuse"; |
| 49 | + |
| 50 | +const server = new RuntimeUseServer({ |
| 51 | + handler: openaiHandler, |
| 52 | + port: 8080, |
| 53 | +}); |
| 54 | + |
| 55 | +await server.startListening(); |
| 56 | +``` |
| 57 | + |
| 58 | +## Custom Handlers |
| 59 | + |
| 60 | +When the built-in handlers are not enough, you can pass your own handler to `RuntimeUseServer`: |
| 61 | + |
| 62 | +```typescript |
| 63 | +import { RuntimeUseServer } from "runtimeuse"; |
| 64 | +import type { |
| 65 | + AgentHandler, |
| 66 | + AgentInvocation, |
| 67 | + AgentResult, |
| 68 | + MessageSender, |
| 69 | +} from "runtimeuse"; |
| 70 | + |
| 71 | +const handler: AgentHandler = { |
| 72 | + async run( |
| 73 | + invocation: AgentInvocation, |
| 74 | + sender: MessageSender, |
| 75 | + ): Promise<AgentResult> { |
| 76 | + sender.sendAssistantMessage(["Running agent..."]); |
| 77 | + |
| 78 | + const output = await myAgent( |
| 79 | + invocation.systemPrompt, |
| 80 | + invocation.userPrompt, |
| 81 | + ); |
| 82 | + |
| 83 | + return { |
| 84 | + type: "structured_output", |
| 85 | + structuredOutput: output, |
| 86 | + metadata: { duration_ms: 1500 }, |
| 87 | + }; |
| 88 | + }, |
| 89 | +}; |
| 90 | + |
| 91 | +const server = new RuntimeUseServer({ handler, port: 8080 }); |
| 92 | +await server.startListening(); |
| 93 | +``` |
| 94 | + |
| 95 | +### Handler Contracts |
| 96 | + |
| 97 | +Your handler receives an `AgentInvocation` with: |
| 98 | + |
| 99 | +| Field | Type | Description | |
| 100 | +| ----- | ---- | ----------- | |
| 101 | +| `systemPrompt` | `string` | System prompt for the agent. | |
| 102 | +| `userPrompt` | `string` | User prompt sent from the Python client. | |
| 103 | +| `model` | `string` | Model name passed by the client. | |
| 104 | +| `outputFormat` | `{ type: "json_schema"; schema: ... } \| undefined` | Present when the client requests structured output. Pass to your agent to enforce the schema. | |
| 105 | +| `signal` | `AbortSignal` | Fires when the client sends a cancel message. Pass to any async operations that support cancellation. | |
| 106 | +| `logger` | `Logger` | Use `invocation.logger.log(msg)` to emit log lines visible in sandbox logs. | |
| 107 | + |
| 108 | +Use `MessageSender` to stream intermediate output before returning the final result: |
| 109 | + |
| 110 | +- `sendAssistantMessage(textBlocks: string[])`: emit text blocks the Python client receives via `on_assistant_message`. |
| 111 | +- `sendErrorMessage(error: string, metadata?: Record<string, unknown>)`: signal a non-fatal error before aborting. |
| 112 | + |
| 113 | +Return an `AgentResult` from your handler: |
| 114 | + |
| 115 | +```typescript |
| 116 | +// Text result |
| 117 | +return { type: "text", text: "...", metadata: { duration_ms: 100 } }; |
| 118 | + |
| 119 | +// Structured output result |
| 120 | +return { type: "structured_output", structuredOutput: { file_count: 42 }, metadata: {} }; |
| 121 | +``` |
| 122 | + |
| 123 | +`metadata` is optional and is passed through to `result.metadata` on the Python side. |
0 commit comments