Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"!dist/**/*.spec.*",
"!dist/examples",
"!dist/test-support",
"!dist/v[12]/**/zod.gen.d.ts",
"schema/v1/schema.json",
"schema/v1/schema.unstable.json",
"schema/v2/schema.json",
Expand All @@ -29,6 +30,7 @@
"LICENSE"
],
"type": "module",
"sideEffects": false,
"main": "dist/v1/stable/acp.js",
"types": "dist/v1/stable/acp.d.ts",
"exports": {
Expand Down Expand Up @@ -100,7 +102,7 @@
"snapshot:nes": "node scripts/snapshot-nes.js",
"typecheck": "tsc --noEmit",
"build": "npm run clean && tsc --project tsconfig.build.json",
"package:check": "node scripts/check-package.js",
"package:check": "node scripts/check-package.js && node scripts/check-bundles.js",
"format": "prettier --write .",
"format:check": "prettier --check .",
"lint": "eslint",
Expand Down Expand Up @@ -134,6 +136,7 @@
"@types/ws": "^8.5.13",
"@typescript-eslint/eslint-plugin": "^8.65.0",
"@typescript-eslint/parser": "^8.65.0",
"esbuild": "^0.28.2",
"eslint": "^10.6.0",
"eslint-config-prettier": "^10.1.8",
"globals": "^17.7.0",
Expand Down
60 changes: 60 additions & 0 deletions scripts/check-bundles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { gzipSync } from "node:zlib";
import { build } from "esbuild";

// Bundle public package imports, including dependencies, as a browser app would.
// Re-export the symbol so minification cannot discard the consumer itself.
for (const [entry, schema] of [
["@agentclientprotocol/sdk", "dist/v1/schema"],
["@agentclientprotocol/sdk/unstable", "dist/v1/unstable/schema"],
["@agentclientprotocol/sdk/experimental/v2", "dist/v2/schema"],
[
"@agentclientprotocol/sdk/experimental/v2/unstable",
"dist/v2/unstable/schema",
],
]) {
for (const name of [
"PROTOCOL_VERSION",
"methods",
"ndJsonStream",
"client",
]) {
const { outputFiles, metafile } = await build({
stdin: {
contents: `export { ${name} } from "${entry}";`,
resolveDir: process.cwd(),
},
bundle: true,
platform: "browser",
format: "esm",
target: "es2022",
minify: true,
write: false,
metafile: true,
});
const { contents } = outputFiles[0];
console.log(
`${entry} ${name}: ${contents.length} bytes minified, ${gzipSync(contents).length} gzip`,
);

// Count modules that contributed bytes, not everything esbuild parsed
// while resolving re-exports. A client needs its own validators, not the
// other protocol versions reached via the optional v2 router.
const inputs = Object.values(metafile.outputs)[0].inputs;
const unexpected = Object.entries(inputs)
.filter(([, { bytesInOutput }]) => bytesInOutput > 0)
.map(([path]) => path)
.filter((path) =>
name === "client"
? path.endsWith("/zod.gen.js") && path !== `${schema}/zod.gen.js`
: path.endsWith("/zod.gen.js") || path.includes("node_modules/zod/"),
);
if (unexpected.length) {
throw new Error(`${entry} ${name} pulled in ${unexpected.join(", ")}`);
}
// Leave room for the helpers to grow without silently bundling the SDK.
const budget = name === "PROTOCOL_VERSION" ? 256 : 8 * 1024;
if (name !== "client" && contents.length > budget) {
throw new Error(`${entry} ${name} exceeded its ${budget}-byte budget`);
}
}
}
3 changes: 3 additions & 0 deletions scripts/check-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const unexpected = [...files].filter(
file.startsWith("dist/") &&
/\.(?:js|js\.map|d\.ts)$/.test(file) &&
!/(?:^|\/)(?:examples|test-support)\//.test(file) &&
// Several MB of inferred validator types, not public in v1 or v2.
// NES does expose its validators; its declarations must stay reachable.
!/^dist\/v[12]\/.*\/zod\.gen\.d\.ts$/.test(file) &&
!/\.(?:test|spec)\./.test(file)
),
);
Expand Down
5 changes: 5 additions & 0 deletions scripts/generate-bindings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { posix as path } from "node:path";

// References between projected modules must stay within the same protocol lane.
export const bindingModules = ["acp", "bindings", "methods"];

const SPEC_TABLES = new Set([
"agentRequestSpecs",
"agentNotificationSpecs",
Expand Down Expand Up @@ -178,6 +181,8 @@ export function generateBindings(ts, template, definitions, metadata, config) {
// Each binding uses its own generated schema, but the same JSON-RPC engine,
// streams and deserialization helpers. Never route through the other API.
if (!specifier.startsWith(".")) return specifier;
if (bindingModules.some((name) => specifier === `./${name}.js`))
return specifier;
const schemaPrefix = "./unstable/schema/";
const target = specifier.startsWith(schemaPrefix)
? path.join(config.outputDir, specifier.slice(schemaPrefix.length))
Expand Down
42 changes: 24 additions & 18 deletions scripts/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as fs from "fs/promises";
import { dirname } from "path";
import * as prettier from "prettier";
import ts from "typescript";
import { generateBindings } from "./generate-bindings.js";
import { bindingModules, generateBindings } from "./generate-bindings.js";

const CURRENT_V1_SCHEMA_RELEASE = "schema-v1.32.0";
const CURRENT_V2_SCHEMA_RELEASE = "schema-v2.0.0-alpha.16";
Expand Down Expand Up @@ -104,8 +104,7 @@ const SCHEMA_CONFIGS = [
expectedExtensibleUnions: V1_EXTENSIBLE_UNIONS,
openApiVersion: "1.0.0",
releaseTag: CURRENT_V1_SCHEMA_RELEASE,
bindingsSource: "./src/v1/acp.ts",
bindingsPath: "./src/v1/stable/acp.ts",
bindingsDir: "./src/v1",
},
{
name: "v1-unstable",
Expand All @@ -132,8 +131,7 @@ const SCHEMA_CONFIGS = [
brandUnknownVariants: true,
openApiVersion: "2.0.0",
releaseTag: CURRENT_V2_SCHEMA_RELEASE,
bindingsSource: "./src/v2/acp.ts",
bindingsPath: "./src/v2/stable/acp.ts",
bindingsDir: "./src/v2",
},
{
name: "v2-unstable",
Expand Down Expand Up @@ -195,22 +193,30 @@ async function main() {
await generateSchema(config, CHECK_GENERATED);
}

for (const config of SCHEMA_CONFIGS.filter((lane) => lane.bindingsPath)) {
const template = await fs.readFile(config.bindingsSource, "utf8");
for (const config of SCHEMA_CONFIGS.filter((lane) => lane.bindingsDir)) {
const schema = JSON.parse(await fs.readFile(config.schemaPath, "utf8"));
const metadata = JSON.parse(await fs.readFile(config.metadataPath, "utf8"));
const bindings = await formatStable(
generateBindings(ts, template, schema.$defs, metadata, config),
);
if (CHECK_GENERATED) {
if ((await fs.readFile(config.bindingsPath, "utf8")) !== bindings) {
throw new Error(
`[${config.name}] ${config.bindingsPath} is stale. Run npm run generate.`,
);
for (const name of bindingModules) {
const bindingsSource = `${config.bindingsDir}/${name}.ts`;
const bindingsPath = `${config.bindingsDir}/stable/${name}.ts`;
const template = await fs.readFile(bindingsSource, "utf8");
const bindings = await formatStable(
generateBindings(ts, template, schema.$defs, metadata, {
...config,
bindingsSource,
bindingsPath,
}),
);
if (CHECK_GENERATED) {
if ((await fs.readFile(bindingsPath, "utf8")) !== bindings) {
throw new Error(
`[${config.name}] ${bindingsPath} is stale. Run npm run generate.`,
);
}
} else {
await fs.mkdir(dirname(bindingsPath), { recursive: true });
await fs.writeFile(bindingsPath, bindings);
}
} else {
await fs.mkdir(dirname(config.bindingsPath), { recursive: true });
await fs.writeFile(config.bindingsPath, bindings);
}
}
}
Expand Down
Loading
Loading