The Open Framework for Building Agents
patch(sandbox): in sandboxes, send an HTTP header identifying eve version (#332)
Marc Codinac5da8e77b0ca
diff --git a/.changeset/sandbox-eve-user-agent.md b/.changeset/sandbox-eve-user-agent.mdnew file mode 100644index 000000000..fac5bec30--- /dev/null+++ b/.changeset/sandbox-eve-user-agent.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Sandbox API requests now append an `eve/<version>` token to the `user-agent` so the sandbox control plane can attribute traffic to eve.diff --git a/packages/eve/src/execution/sandbox/bindings/vercel-create-sdk.ts b/packages/eve/src/execution/sandbox/bindings/vercel-create-sdk.tsindex 43c08cad6..43f3deb15 100644--- a/packages/eve/src/execution/sandbox/bindings/vercel-create-sdk.ts+++ b/packages/eve/src/execution/sandbox/bindings/vercel-create-sdk.ts@@ -1,3 +1,4 @@+import { getVercelSandboxFetch } from "#execution/sandbox/bindings/vercel-credentials.js";import type {VercelCreateOptions,VercelModule,@@ -25,6 +26,7 @@ export async function createVercelEveImageSandbox(input: {readonly sandboxModule: VercelModule;}): Promise<VercelSandbox> {const { image: _image, runtime: _runtime, source, ...createOptions } = input.createOptions;+ const fetch = getVercelSandboxFetch(input.createOptions);/** `runtime`, `image`, and a snapshot source are mutually exclusive in the@@ -34,12 +36,14 @@ export async function createVercelEveImageSandbox(input: {return await input.sandboxModule.Sandbox.create({...createOptions,source,+ fetch,});}return await input.sandboxModule.Sandbox.create({...createOptions,source,image: VERCEL_EVE_SANDBOX_IMAGE,+ fetch,});}diff --git a/packages/eve/src/execution/sandbox/bindings/vercel-credentials.ts b/packages/eve/src/execution/sandbox/bindings/vercel-credentials.tsindex c0b5578f4..f5224c2ef 100644--- a/packages/eve/src/execution/sandbox/bindings/vercel-credentials.ts+++ b/packages/eve/src/execution/sandbox/bindings/vercel-credentials.ts@@ -1,9 +1,10 @@import { getVercelOidcToken } from "#compiled/@vercel/oidc/index.js";+import { withEveSandboxUserAgent } from "#execution/sandbox/bindings/vercel-user-agent.js";import type { VercelCreateOptions } from "#execution/sandbox/bindings/vercel-sdk-types.js";export function getVercelSandboxFetch(createOptions: VercelCreateOptions): typeof globalThis.fetch {const fetchOverride = (createOptions as { readonly fetch?: typeof globalThis.fetch }).fetch;- return fetchOverride ?? globalThis.fetch;+ return withEveSandboxUserAgent(fetchOverride ?? globalThis.fetch);}export async function getVercelSandboxCredentials(diff --git a/packages/eve/src/execution/sandbox/bindings/vercel-user-agent.test.ts b/packages/eve/src/execution/sandbox/bindings/vercel-user-agent.test.tsnew file mode 100644index 000000000..a49fa6aa9--- /dev/null+++ b/packages/eve/src/execution/sandbox/bindings/vercel-user-agent.test.ts@@ -0,0 +1,36 @@+import { describe, expect, it, vi } from "vitest";++import { withEveSandboxUserAgent } from "#execution/sandbox/bindings/vercel-user-agent.js";++function userAgentOf(init: RequestInit | undefined): string | null {+ return new Headers(init?.headers).get("user-agent");+}++describe("withEveSandboxUserAgent", () => {+ it("appends the eve token to an existing user-agent", async () => {+ const inner = vi.fn<typeof globalThis.fetch>().mockResolvedValue(new Response());+ const wrapped = withEveSandboxUserAgent(inner);++ await wrapped("https://api.vercel.com/sandboxes", {+ headers: { "user-agent": "vercel/sandbox/2.2.0" },+ });++ const [, init] = inner.mock.calls[0]!;+ expect(userAgentOf(init)).toMatch(/^vercel\/sandbox\/2\.2\.0 eve\/.+/);+ });++ it("sets the eve token as the user-agent when none is present", async () => {+ const inner = vi.fn<typeof globalThis.fetch>().mockResolvedValue(new Response());+ const wrapped = withEveSandboxUserAgent(inner);++ await wrapped("https://api.vercel.com/sandboxes");++ const [, init] = inner.mock.calls[0]!;+ expect(userAgentOf(init)).toMatch(/^eve\/.+/);+ });++ it("delegates to globalThis.fetch when no inner fetch is supplied", () => {+ const wrapped = withEveSandboxUserAgent();+ expect(typeof wrapped).toBe("function");+ });+});diff --git a/packages/eve/src/execution/sandbox/bindings/vercel-user-agent.ts b/packages/eve/src/execution/sandbox/bindings/vercel-user-agent.tsnew file mode 100644index 000000000..cbf623022--- /dev/null+++ b/packages/eve/src/execution/sandbox/bindings/vercel-user-agent.ts@@ -0,0 +1,28 @@+import { resolveInstalledPackageInfo } from "#internal/application/package.js";++export function eveSandboxUserAgentToken(): string {+ const { name, version } = resolveInstalledPackageInfo();+ return `${name}/${version}`;+}++/**+ * Wraps a `fetch` implementation so every request's `user-agent` ends with the+ * {@link eveSandboxUserAgentToken} (e.g.: eve/0.18.1).+ */+export function withEveSandboxUserAgent(+ inner: typeof globalThis.fetch = globalThis.fetch,+): typeof globalThis.fetch {+ const token = eveSandboxUserAgentToken();++ return (input, init) => {+ const headers = new Headers(+ init?.headers ??+ (typeof input === "object" && input !== null && "headers" in input+ ? (input as Request).headers+ : undefined),+ );+ const existing = headers.get("user-agent");+ headers.set("user-agent", existing ? `${existing} ${token}` : token);+ return inner(input, { ...init, headers });+ };+}