@agent-device/proxy

中文文档
0.21.38发布于 2026/11/17MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2万

Transport-neutral authenticating proxy in front of an agent-device daemon.

原始使用文档
文档目录8 节
  1. Install
  2. Serve the proxy over HTTP
  3. Bring your own transport
  4. Serve uploads through a TLS tunnel
  5. Reach the daemon over your own transport
  6. Authenticate each client yourself
  7. Options
  8. Health and restarts

@agent-device/proxy

The proxy behind agent-device proxy, as a library. Embed it in your own gateway to give remote agent-device clients access to a daemon on another machine, over whichever transport you choose.

The proxy:

  • authenticates clients with a token you choose, or with your own check, and never exposes the daemon's own token;
  • forwards only the routes a remote client needs: /health, /rpc, uploads, artifacts, and request diagnostics;
  • refuses install sources that name a file on the daemon host;
  • rewrites upload tickets so clients upload through the proxy, not to the daemon directly.

Requires Node.js 22.12 or later.

Install

npm install @agent-device/proxy

Keep its version close to the daemon behind it. Clients refuse a proxy whose daemon speaks a different RPC protocol version, before any command runs.

Serve the proxy over HTTP

import { createDaemonProxyServer } from '@agent-device/proxy';

const server = createDaemonProxyServer({
  upstreamBaseUrl: 'http://127.0.0.1:4310',
  upstreamToken: process.env.AGENT_DEVICE_DAEMON_TOKEN!,
  clientToken: process.env.GATEWAY_CLIENT_TOKEN!,
});
server.listen(8080);

Clients connect with agent-device connect proxy --daemon-base-url https://gateway.example.com/agent-device and AGENT_DEVICE_DAEMON_AUTH_TOKEN set to clientToken. Each agent sets its own AGENT_DEVICE_SESSION (or passes --session): a command that names no session on a proxy connection is refused.

To mount the proxy in an existing node:http or node:https server, use createDaemonProxyRequestListener(createDaemonProxy(options)).

Bring your own transport

createDaemonProxy has no server of its own. It answers standard Request objects with standard Response objects, so any Node.js-compatible runtime or framework that speaks the Fetch API can host it:

import { createDaemonProxy } from '@agent-device/proxy';

const proxy = createDaemonProxy({
  upstreamBaseUrl: 'http://127.0.0.1:4310',
  upstreamToken: daemonToken,
  clientToken,
});

// A fetch-style server entry, as used by Bun and by Hono on Node.js
export default { fetch: proxy.handle };

For a WebSocket or other message transport, turn each incoming message into a Request and send the Response back. Three things the proxy relies on:

  • Without publicOrigin, construct request.url with the canonical client-facing URL for each request. The embedder is responsible for that URL. Forwarded and X-Forwarded-* headers do not select the upload ticket origin.
  • request.signal aborts when the client goes away. The proxy then cancels the daemon request, so work the client stopped waiting for does not keep running.
  • Response bodies stream. Artifact downloads can be large, so forward the body as it arrives.

handle never rejects. Errors come back as JSON responses with a 4xx or 5xx status, except a route the proxy does not serve, which gets a plain Not found 404.

Serve uploads through a TLS tunnel

Set publicOrigin to the public HTTP(S) origin when TLS terminates before the proxy, for example https://public.example:8443. Use the scheme, host and optional port, without credentials, a path, query or fragment. With the CLI, run agent-device proxy --port 4310 --public-origin https://public.example:8443. For an ephemeral tunnel, obtain its public name first, then configure and restart the proxy. This replaces the previous first-X-Forwarded-Proto behavior.

Without a fixed origin, the Node listener uses the actual socket scheme and one validated Host header. Actual Node HTTPS sockets are supported. A custom direct-daemon HTTP embedding behind TLS termination can pass publicOrigin to createDaemonHttpServer. The built-in daemon listens on loopback HTTP; expose it behind TLS through a configured proxy.

Your Node HTTP client must send exactly one valid Host header on every request, including /health. Missing, duplicate or malformed Host headers receive HTTP 400, even with publicOrigin configured. Configure raw-socket and HTTP/1.0 health probes to send Host: localhost (or your deployment's host and port).

Reach the daemon over your own transport

By default the proxy calls the daemon with the global fetch. Pass upstreamFetch when the daemon is only reachable some other way, such as a tunnel the device host opened to your gateway:

const proxy = createDaemonProxy({
  upstreamBaseUrl: 'http://device-host-17.internal',
  upstreamToken: daemonToken,
  clientToken,
  upstreamFetch: (request) => deviceHostTunnel.send(request),
});

upstreamFetch receives a Request addressed to upstreamBaseUrl and must resolve with the daemon's Response. Abort the exchange when request.signal aborts.

Authenticate each client yourself

One clientToken is shared by every client. To give each client its own credential, pass authenticate instead. It receives each client request (with its JSON body on /rpc) and returns the tenant the client acts as, or null to refuse it with a 401:

const proxy = createDaemonProxy({
  upstreamBaseUrl: 'http://127.0.0.1:4310',
  upstreamToken: daemonToken,
  authenticate: async (request) => {
    const account = await accounts.findByApiKey(request.headers.get('authorization'));
    return account ? { tenantId: account.id } : null;
  },
});

The proxy then sends every authenticated request to the daemon as that tenant, whatever tenant the client named, and isolates its sessions from other tenants' sessions. GET /health is not authenticated and carries no tenant. A tenant id is 1-128 letters, numbers, dots, underscores or hyphens; any other id fails the request with a 500. On /rpc the proxy accepts only agent_device.command and the agent_device.lease.* methods (or the same names spelled agent-device.command and agent-device.lease.*), and refuses others with a 401.

Clients upload files with a ticket that names the headers to send. The proxy builds that ticket from the daemon ticket's content-type and the client's own credential, so it never carries the daemon's token. By default the credential is the client's authorization and x-agent-device-token headers. If your clients use another header, such as a cookie or an API key header, return it as uploadHeaders so the upload passes authenticate too:

authenticate: async (request) => {
  const apiKey = request.headers.get('x-api-key');
  if (!apiKey) return null;
  const account = await accounts.findByApiKey(apiKey);
  return account ? { tenantId: account.id, uploadHeaders: { 'x-api-key': apiKey } } : null;
},

Options

OptionDefaultMeaning
upstreamBaseUrlrequiredBase URL of the daemon's HTTP server.
upstreamTokenrequiredThe daemon's auth token.
clientTokenrequired without authenticateToken clients must send as their daemon auth token.
authenticatenoneAuthenticates each client and names its tenant; replaces clientToken.
upstreamFetchglobal fetchSends one request to the daemon.
maxRpcBodyBytes64 MiB, the daemon's own request limitLarger /rpc bodies get a 400 response.
upstreamTimeoutMsnoneLongest time one daemon request may take, including its body. Without it, a request lasts as long as the client waits, and the client's command timeout bounds it.

maxRpcBodyBytes limits the bytes of one /rpc body. The proxy reads the whole body before it authenticates the client, then decodes and parses it. One unauthenticated request can therefore hold more memory than the limit: the raw bytes plus the decoded string and the parsed request. For a proxy that faces the internet, set maxRpcBodyBytes lower than the 64 MiB default, to the largest request your clients send.

After the limit check, the proxy rewrites the body: it adds the daemon token and, with authenticate, the tenant fields. The rewritten body can be a few hundred bytes larger than the client's. The daemon applies its own 64 MiB limit, so a client body within that margin of 64 MiB passes the proxy and the daemon refuses it. Clients refuse requests over 64 MiB before sending them, so only a body in that narrow margin is affected.

Health and restarts

GET /health (also served as /agent-device/health) needs no token. It reports the proxy's version and instanceId, and includes the daemon's own health under upstream. A new createDaemonProxy call gets a new instanceId, so connected clients notice the restart and re-check the connection before sending more commands.

版本与发布记录

@agent-device/proxy 共发布 51 个版本,首次发布于 2026/10/07;过去 12 个月发布了 51 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.21.38latest2026/11/17今天
0.22.0-nightly-0-22.20261116.179nightly-0.222026/11/16今天
0.21.38-nightly.20261113.167nightly2026/11/133 天前
0.21.372026/11/133 天前
0.21.37-nightly.20261111.1612026/11/115 天前
0.21.37-nightly.20261111.1572026/11/115 天前

关于 @agent-device/proxy 的常见问题

@agent-device/proxy 是什么?

Transport-neutral authenticating proxy in front of an agent-device daemon.

如何安装 @agent-device/proxy?

在项目目录运行 npm install @agent-device/proxy,或使用 pnpm add / yarn add / bun add。

@agent-device/proxy 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@agent-device/proxy 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@agent-device/proxy 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@agent-device/proxy 可以商用吗?

@agent-device/proxy 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@agent-device/proxy 的下载量有多少?

根据 npm 官方下载统计,@agent-device/proxy 在 2026/10/05 – 2026/11/03 这 30 天内被下载 20,367 次,日均约 679 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@agent-device/proxy 还在维护吗?

最新版本 0.21.38 发布于 2026/11/17(今天);至今共发布 51 个版本,其中过去 12 个月发布了 51 个;首次发布于 2026/10/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。